
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta charset="utf-8" />
    <title>datetime --- 基本的日期和时间类型 &#8212; Python 3.7.8 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.8 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="calendar --- 日历相关函数" href="calendar.html" />
    <link rel="prev" title="数据类型" href="datatypes.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/datetime.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="calendar.html" title="calendar --- 日历相关函数"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="datatypes.html" title="数据类型"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="datatypes.html" accesskey="U">数据类型</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-datetime">
<span id="datetime-basic-date-and-time-types"></span><h1><a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code></a> --- 基本的日期和时间类型<a class="headerlink" href="#module-datetime" title="永久链接至标题">¶</a></h1>
<p><strong>源代码：</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.7/Lib/datetime.py">Lib/datetime.py</a></p>
<hr class="docutils" />
<p><a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code></a>  模块提供了可以通过多种方式操作日期和时间的类。在支持日期时间数学运算的同时，实现的关注点更着重于如何能够更有效地解析其属性用于格式化输出和数据操作。相关功能可以参阅 <a class="reference internal" href="time.html#module-time" title="time: Time access and conversions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">time</span></code></a> 和 <a class="reference internal" href="calendar.html#module-calendar" title="calendar: Functions for working with calendars, including some emulation of the Unix cal program."><code class="xref py py-mod docutils literal notranslate"><span class="pre">calendar</span></code></a>  模块。</p>
<p>有两种日期和时间的对象：“简单型“和”感知型“。</p>
<p>感知型对象有着用足以支持一些应用层面算法和国家层面时间调整的信息，例如时区和夏令时，来让自己和其他的感知型对象区别开来。感知型对象是用来表达不对解释器开放的特定时间信息 <a class="footnote-reference brackets" href="#id3" id="id1">1</a>。</p>
<p>简单型对象没包含足够多的信息来明确定位与之相关的  date/time 对象。 简单型对象所代表的是世界标准时间(UTC)、当地时间或者是其它时区的时间完全取决于程序，就像一个数字是代表的是米、英里或者质量完全取决于程序一样。 简单型对象以忽略了一些现实情况的为代价使得它容易理解和使用。</p>
<p>对于需要感知型对象的应用， <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象和 <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> 对象有一个可选的时区信息属性 <code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code> ，这个属性可以设置给 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 类的子类实例。这些 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 对象捕获关于相对于世界标准时间（UTC）偏移、时区名字和夏令时是否有效等信息。需要注意的是，只有一个具体的 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 类，即由 <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code></a> 模块提供的 <a class="reference internal" href="#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a> 类。 <a class="reference internal" href="#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a> 类可以代表相对于世界标准时间（UTC）固定偏移的简单时区，比如世界标准时间（UTC）自己或者北美东部时间或者东部夏令时。支持时区的详细程度取决于应用。世界各地的时间调整规则相比理性更加政治性，经常会变更。也没有一个基于世界标准时间（UTC）的标准套件适合用于所有应用。</p>
<p>The <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code></a> module exports the following constants:</p>
<dl class="data">
<dt id="datetime.MINYEAR">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">MINYEAR</code><a class="headerlink" href="#datetime.MINYEAR" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 或者 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象允许的最小年份。 常量 <a class="reference internal" href="#datetime.MINYEAR" title="datetime.MINYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MINYEAR</span></code></a> 是 <code class="docutils literal notranslate"><span class="pre">1</span></code> 。</p>
</dd></dl>

<dl class="data">
<dt id="datetime.MAXYEAR">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">MAXYEAR</code><a class="headerlink" href="#datetime.MAXYEAR" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 或 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象允许最大的年份。常量 <a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MAXYEAR</span></code></a> 是 <code class="docutils literal notranslate"><span class="pre">9999</span></code> 。</p>
</dd></dl>

<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt>模块 <a class="reference internal" href="calendar.html#module-calendar" title="calendar: Functions for working with calendars, including some emulation of the Unix cal program."><code class="xref py py-mod docutils literal notranslate"><span class="pre">calendar</span></code></a></dt><dd><p>日历相关函数</p>
</dd>
<dt>模块 <a class="reference internal" href="time.html#module-time" title="time: Time access and conversions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">time</span></code></a></dt><dd><p>时间的访问和转换</p>
</dd>
</dl>
</div>
<div class="section" id="available-types">
<h2>有效的类型<a class="headerlink" href="#available-types" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt>
<em class="property">class </em><code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">date</code></dt>
<dd><p>一个理想化的简单型日期，它假设当今的公历在过去和未来永远有效。 属性: <a class="reference internal" href="#datetime.date.year" title="datetime.date.year"><code class="xref py py-attr docutils literal notranslate"><span class="pre">year</span></code></a>, <a class="reference internal" href="#datetime.date.month" title="datetime.date.month"><code class="xref py py-attr docutils literal notranslate"><span class="pre">month</span></code></a>, and <a class="reference internal" href="#datetime.date.day" title="datetime.date.day"><code class="xref py py-attr docutils literal notranslate"><span class="pre">day</span></code></a>。</p>
</dd></dl>

<dl class="class">
<dt>
<em class="property">class </em><code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">time</code></dt>
<dd><p>一个理想化的时间，它独立于任何特定的日期，假设每天一共有 24*60*60 秒（这里没有&quot;闰秒&quot;的概念）。 属性： <a class="reference internal" href="#datetime.time.hour" title="datetime.time.hour"><code class="xref py py-attr docutils literal notranslate"><span class="pre">hour</span></code></a>, <a class="reference internal" href="#datetime.time.minute" title="datetime.time.minute"><code class="xref py py-attr docutils literal notranslate"><span class="pre">minute</span></code></a>, <a class="reference internal" href="#datetime.time.second" title="datetime.time.second"><code class="xref py py-attr docutils literal notranslate"><span class="pre">second</span></code></a>, <a class="reference internal" href="#datetime.time.microsecond" title="datetime.time.microsecond"><code class="xref py py-attr docutils literal notranslate"><span class="pre">microsecond</span></code></a>, 和 <a class="reference internal" href="#datetime.time.tzinfo" title="datetime.time.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a>。</p>
</dd></dl>

<dl class="class">
<dt>
<em class="property">class </em><code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">datetime</code></dt>
<dd><p>日期和时间的结合。属性：<a class="reference internal" href="#datetime.datetime.year" title="datetime.datetime.year"><code class="xref py py-attr docutils literal notranslate"><span class="pre">year</span></code></a>, <a class="reference internal" href="#datetime.datetime.month" title="datetime.datetime.month"><code class="xref py py-attr docutils literal notranslate"><span class="pre">month</span></code></a>, <a class="reference internal" href="#datetime.datetime.day" title="datetime.datetime.day"><code class="xref py py-attr docutils literal notranslate"><span class="pre">day</span></code></a>, <a class="reference internal" href="#datetime.datetime.hour" title="datetime.datetime.hour"><code class="xref py py-attr docutils literal notranslate"><span class="pre">hour</span></code></a>, <a class="reference internal" href="#datetime.datetime.minute" title="datetime.datetime.minute"><code class="xref py py-attr docutils literal notranslate"><span class="pre">minute</span></code></a>, <a class="reference internal" href="#datetime.datetime.second" title="datetime.datetime.second"><code class="xref py py-attr docutils literal notranslate"><span class="pre">second</span></code></a>, <a class="reference internal" href="#datetime.datetime.microsecond" title="datetime.datetime.microsecond"><code class="xref py py-attr docutils literal notranslate"><span class="pre">microsecond</span></code></a>, and <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a>.</p>
</dd></dl>

<dl class="class">
<dt>
<em class="property">class </em><code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">timedelta</code></dt>
<dd><p>表示两个 <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 对象或者 <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> 对象,或者 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象之间的时间间隔，精确到微秒。</p>
</dd></dl>

<dl class="class">
<dt>
<em class="property">class </em><code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">tzinfo</code></dt>
<dd><p>一个描述时区信息的抽象基类。用于给 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 类和 <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> 类提供自定义的时间调整概念（例如，负责时区或者夏令时）。</p>
</dd></dl>

<dl class="class">
<dt>
<em class="property">class </em><code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">timezone</code></dt>
<dd><p>一个实现了 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 抽象基类的子类，用于表示相对于 世界标准时间（UTC）的偏移量。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.2 新版功能.</span></p>
</div>
</dd></dl>

<p>这些类型的对象都是不可变的。</p>
<p><a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a>  类型的对象都是简单型的。</p>
<p>An object of type <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> or <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> may be naive or aware.
A <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> object <em>d</em> is aware if <code class="docutils literal notranslate"><span class="pre">d.tzinfo</span></code> is not <code class="docutils literal notranslate"><span class="pre">None</span></code> and
<code class="docutils literal notranslate"><span class="pre">d.tzinfo.utcoffset(d)</span></code> does not return <code class="docutils literal notranslate"><span class="pre">None</span></code>.  If <code class="docutils literal notranslate"><span class="pre">d.tzinfo</span></code> is
<code class="docutils literal notranslate"><span class="pre">None</span></code>, or if <code class="docutils literal notranslate"><span class="pre">d.tzinfo</span></code> is not <code class="docutils literal notranslate"><span class="pre">None</span></code> but <code class="docutils literal notranslate"><span class="pre">d.tzinfo.utcoffset(d)</span></code>
returns <code class="docutils literal notranslate"><span class="pre">None</span></code>, <em>d</em> is naive.  A <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> object <em>t</em> is aware
if <code class="docutils literal notranslate"><span class="pre">t.tzinfo</span></code> is not <code class="docutils literal notranslate"><span class="pre">None</span></code> and <code class="docutils literal notranslate"><span class="pre">t.tzinfo.utcoffset(None)</span></code> does not return
<code class="docutils literal notranslate"><span class="pre">None</span></code>.  Otherwise, <em>t</em> is naive.</p>
<p>简单型和感知型之间的差别不适用于 <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 对象。</p>
<p>子类关系</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nb">object</span>
    <span class="n">timedelta</span>
    <span class="n">tzinfo</span>
        <span class="n">timezone</span>
    <span class="n">time</span>
    <span class="n">date</span>
        <span class="n">datetime</span>
</pre></div>
</div>
</div>
<div class="section" id="timedelta-objects">
<span id="datetime-timedelta"></span><h2><a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 类对象<a class="headerlink" href="#timedelta-objects" title="永久链接至标题">¶</a></h2>
<p><a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 对象表示两个 date 或者 time 的时间间隔。</p>
<dl class="class">
<dt id="datetime.timedelta">
<em class="property">class </em><code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">timedelta</code><span class="sig-paren">(</span><em class="sig-param">days=0</em>, <em class="sig-param">seconds=0</em>, <em class="sig-param">microseconds=0</em>, <em class="sig-param">milliseconds=0</em>, <em class="sig-param">minutes=0</em>, <em class="sig-param">hours=0</em>, <em class="sig-param">weeks=0</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.timedelta" title="永久链接至目标">¶</a></dt>
<dd><p>所有的参数都是可选的并且默认为 <code class="docutils literal notranslate"><span class="pre">0</span></code>。这些参数可以是整数或者浮点数，也可以是正数或者负数。</p>
<p>只有 <em>days</em>,*seconds* 和 <em>microseconds</em> 会存储在内部，即python内部以 <em>days</em>,*seconds* 和 <em>microseconds</em> 三个单位作为存储的基本单位。参数单位转换规则如下：</p>
<ul class="simple">
<li><p>1毫秒会转换成1000微秒。</p></li>
<li><p>1分钟会转换成60秒。</p></li>
<li><p>1小时会转换成3600秒。</p></li>
<li><p>1星期会转换成7天。</p></li>
</ul>
<p>days, seconds, microseconds 本身也是标准化的，以保证表达方式的唯一性，例：</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre">&lt;=</span> <span class="pre">microseconds</span> <span class="pre">&lt;</span> <span class="pre">1000000</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre">&lt;=</span> <span class="pre">seconds</span> <span class="pre">&lt;</span> <span class="pre">3600*24</span></code> (一天的秒数)</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">-999999999</span> <span class="pre">&lt;=</span> <span class="pre">days</span> <span class="pre">&lt;=</span> <span class="pre">999999999</span></code></p></li>
</ul>
<p>在有任何浮点型参数或者微秒为小数的情况下，所有小数均会按照前面的换算规则叠加到下一级，并使用round-half-to-even的方法对微秒进行取舍。没有浮点型参数情况下，转换的过程就是精确的（没有信息丢失）。</p>
<p>如果标准化后的 days 数值超过了指定范围，将会抛出 <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a> 异常。</p>
<p>需要注意的是，负数被标准化后的结果会让你大吃一惊。例如，</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">timedelta</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">microseconds</span><span class="o">=-</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">days</span><span class="p">,</span> <span class="n">d</span><span class="o">.</span><span class="n">seconds</span><span class="p">,</span> <span class="n">d</span><span class="o">.</span><span class="n">microseconds</span><span class="p">)</span>
<span class="go">(-1, 86399, 999999)</span>
</pre></div>
</div>
</dd></dl>

<p>类属性：</p>
<dl class="attribute">
<dt id="datetime.timedelta.min">
<code class="sig-prename descclassname">timedelta.</code><code class="sig-name descname">min</code><a class="headerlink" href="#datetime.timedelta.min" title="永久链接至目标">¶</a></dt>
<dd><p>The most negative <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object, <code class="docutils literal notranslate"><span class="pre">timedelta(-999999999)</span></code>.</p>
</dd></dl>

<dl class="attribute">
<dt id="datetime.timedelta.max">
<code class="sig-prename descclassname">timedelta.</code><code class="sig-name descname">max</code><a class="headerlink" href="#datetime.timedelta.max" title="永久链接至目标">¶</a></dt>
<dd><p>The most positive <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object, <code class="docutils literal notranslate"><span class="pre">timedelta(days=999999999,</span>
<span class="pre">hours=23,</span> <span class="pre">minutes=59,</span> <span class="pre">seconds=59,</span> <span class="pre">microseconds=999999)</span></code>.</p>
</dd></dl>

<dl class="attribute">
<dt id="datetime.timedelta.resolution">
<code class="sig-prename descclassname">timedelta.</code><code class="sig-name descname">resolution</code><a class="headerlink" href="#datetime.timedelta.resolution" title="永久链接至目标">¶</a></dt>
<dd><p>两个不相等的 <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 类对象最小的间隔为 <code class="docutils literal notranslate"><span class="pre">timedelta(microseconds=1)</span></code>。</p>
</dd></dl>

<p>需要注意的是，因为标准化的缘故，<code class="docutils literal notranslate"><span class="pre">timedelta.max</span></code> &gt; <code class="docutils literal notranslate"><span class="pre">-timedelta.min</span></code>，<code class="docutils literal notranslate"><span class="pre">-timedelta.max</span></code> 不可以表示一个 <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 类对象。</p>
<p>实例属性（只读）：</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 29%" />
<col style="width: 71%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>属性</p></th>
<th class="head"><p>值</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">days</span></code></p></td>
<td><p>-999999999 至 999999999 ，含999999999</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">seconds</span></code></p></td>
<td><p>0 至 86399，包含86399</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">microseconds</span></code></p></td>
<td><p>0 至 999999，包含999999</p></td>
</tr>
</tbody>
</table>
<p>支持的运算：</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 41%" />
<col style="width: 59%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>运算</p></th>
<th class="head"><p>结果：</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">+</span> <span class="pre">t3</span></code></p></td>
<td><p><em>t2</em> 和 <em>t3</em> 的和。 运算后 <em>t1</em>-<em>t2</em> == <em>t3</em> and <em>t1</em>-<em>t3</em> == <em>t2</em> 必为真值。(1)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">-</span> <span class="pre">t3</span></code></p></td>
<td><p><em>t2</em> 减 <em>t3</em> 的差。 运算后 <em>t1</em> == <em>t2</em> - <em>t3</em> and <em>t2</em> == <em>t1</em> + <em>t3</em> 必为真值。 (1)(6)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">*</span> <span class="pre">i</span> <span class="pre">or</span> <span class="pre">t1</span> <span class="pre">=</span> <span class="pre">i</span> <span class="pre">*</span> <span class="pre">t2</span></code></p></td>
<td><p>乘以一个整数。运算后假如 <code class="docutils literal notranslate"><span class="pre">i</span> <span class="pre">!=</span> <span class="pre">0</span></code> 则 <em>t1</em> // i == <em>t2</em> 必为真值。</p></td>
</tr>
<tr class="row-odd"><td></td>
<td><p>In general, <em>t1</em> * i == <em>t1</em> * (i-1) + <em>t1</em>
is true. (1)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">*</span> <span class="pre">f</span> <span class="pre">or</span> <span class="pre">t1</span> <span class="pre">=</span> <span class="pre">f</span> <span class="pre">*</span> <span class="pre">t2</span></code></p></td>
<td><p>乘以一个浮点数，结果会被舍入到 timedelta 最接近的整数倍。 精度使用四舍五偶入奇不入规则。</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">f</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">/</span> <span class="pre">t3</span></code></p></td>
<td><p>总时间 <em>t2</em> 除以间隔单位 <em>t3</em> (3)。 返回一个 <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a> 对象。</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">/</span> <span class="pre">f</span> <span class="pre">or</span> <span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">/</span> <span class="pre">i</span></code></p></td>
<td><p>除以一个浮点数或整数。 结果会被舍入到 timedelta 最接近的整数倍。 精度使用四舍五偶入奇不入规则。</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">//</span> <span class="pre">i</span></code> or
<code class="docutils literal notranslate"><span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">//</span> <span class="pre">t3</span></code></p></td>
<td><p>取整除，余数部分(如果有的话)将被丢弃。在第二种情况下，返回一个整数。(3)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">%</span> <span class="pre">t3</span></code></p></td>
<td><p>余数为一个 <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 对象。(3)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">q,</span> <span class="pre">r</span> <span class="pre">=</span> <span class="pre">divmod(t1,</span> <span class="pre">t2)</span></code></p></td>
<td><p>通过 : <code class="docutils literal notranslate"><span class="pre">q</span> <span class="pre">=</span> <span class="pre">t1</span> <span class="pre">//</span> <span class="pre">t2</span></code> (3) and <code class="docutils literal notranslate"><span class="pre">r</span> <span class="pre">=</span> <span class="pre">t1</span> <span class="pre">%</span> <span class="pre">t2</span></code> 计算出商和余数。q是一个整数，r是一个 <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 对象。</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">+t1</span></code></p></td>
<td><p>返回一个相同数值的 <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 对象。</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">-t1</span></code></p></td>
<td><p>等价于 <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a>(-<em>t1.days</em>, -<em>t1.seconds</em>, -<em>t1.microseconds</em>), 和 <em>t1</em>* -1. (1)(4)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">abs(t)</span></code></p></td>
<td><p>当 <code class="docutils literal notranslate"><span class="pre">t.days</span> <span class="pre">&gt;=</span> <span class="pre">0``时等于</span> <span class="pre">+\</span> <span class="pre">*t*,</span> <span class="pre">当</span> <span class="pre">``t.days</span> <span class="pre">&lt;</span> <span class="pre">0</span></code> 时 -<em>t</em> 。 (2)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">str(t)</span></code></p></td>
<td><p>返回一个形如 <code class="docutils literal notranslate"><span class="pre">[D</span> <span class="pre">day[s],</span> <span class="pre">][H]H:MM:SS[.UUUUUU]</span></code> 的字符串，当 <code class="docutils literal notranslate"><span class="pre">t</span></code> 为负数的时候，  D 也为负数。 (5)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">repr(t)</span></code></p></td>
<td><p>返回一个 <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 对象的字符串表示形式，作为附带正规属性值的构造器调用。</p></td>
</tr>
</tbody>
</table>
<p>注释:</p>
<ol class="arabic">
<li><p>精确但可能会溢出。</p></li>
<li><p>精确且不会溢出。</p></li>
<li><p>除以0将会抛出异常  <a class="reference internal" href="exceptions.html#ZeroDivisionError" title="ZeroDivisionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ZeroDivisionError</span></code></a> 。</p></li>
<li><p>-<em>timedelta.max</em> 不是一个 <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 类对象。</p></li>
<li><p>String representations of <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> objects are normalized
similarly to their internal representation.  This leads to somewhat
unusual results for negative timedeltas.  For example:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=-</span><span class="mi">5</span><span class="p">)</span>
<span class="go">datetime.timedelta(days=-1, seconds=68400)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">_</span><span class="p">)</span>
<span class="go">-1 day, 19:00:00</span>
</pre></div>
</div>
</li>
<li><p>表达式 <code class="docutils literal notranslate"><span class="pre">t2</span> <span class="pre">-</span> <span class="pre">t3</span></code> 通常与 <code class="docutils literal notranslate"><span class="pre">t2</span> <span class="pre">+</span> <span class="pre">(-t3)</span></code> 是等价的，除非 t3 等于 <code class="docutils literal notranslate"><span class="pre">timedelta.max</span></code>; 在这种情况下前者会返回结果，而后者则会溢出。</p></li>
</ol>
<p>除了上面列举的操作以外 <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 对象还支持与 <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 和 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象进行特定的相加和相减运算（见下文）。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.2 版更改: </span>现在已支持 <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 对象与另一个 <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 对象相整除或相除，包括求余运算和 <a class="reference internal" href="functions.html#divmod" title="divmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">divmod()</span></code></a> 函数。 现在也支持 <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 对象被 <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a> 对象除或乘。</p>
</div>
<p><a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 对象与 <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 对象比较的支持是通过将表示较小时间差的 timedelta 对象视为较小值。 为了防止将混合类型比较回退为基于对象地址的默认比较，当 <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 对象与不同类型的对象比较时，将会引发 <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>，除非比较运算符是 <code class="docutils literal notranslate"><span class="pre">==</span></code> 或 <code class="docutils literal notranslate"><span class="pre">!=</span></code>。 在后一种情况下将分别返回 <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a> 或 <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a>。</p>
<p><a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 对象是 <a class="reference internal" href="../glossary.html#term-hashable"><span class="xref std std-term">hashable</span></a>  类型的(可以作为字典关键字), 支持高效获取, 在布尔上下文中， <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 对象大多数情况下都被视为真，仅在不等于 <code class="docutils literal notranslate"><span class="pre">timedelta(0)</span></code> 时。</p>
<p>实例方法：</p>
<dl class="method">
<dt id="datetime.timedelta.total_seconds">
<code class="sig-prename descclassname">timedelta.</code><code class="sig-name descname">total_seconds</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.timedelta.total_seconds" title="永久链接至目标">¶</a></dt>
<dd><p>返回时间间隔包含了多少秒。造价于 <code class="docutils literal notranslate"><span class="pre">td</span> <span class="pre">/</span> <span class="pre">timedelta(seconds=1)</span></code>。对于其它单位可以直接使用除法的形式 (例如  <code class="docutils literal notranslate"><span class="pre">td</span> <span class="pre">/</span> <span class="pre">timedelta(microseconds=1)</span></code>)。</p>
<p>需要注意的是，时间间隔较大时，这个方法的结果中的微秒将会失真（大多数平台上大于270年视为一个较大的时间间隔）。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.2 新版功能.</span></p>
</div>
</dd></dl>

<p>用法示例：</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">timedelta</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">year</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="mi">365</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">another_year</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">weeks</span><span class="o">=</span><span class="mi">40</span><span class="p">,</span> <span class="n">days</span><span class="o">=</span><span class="mi">84</span><span class="p">,</span> <span class="n">hours</span><span class="o">=</span><span class="mi">23</span><span class="p">,</span>
<span class="gp">... </span>                         <span class="n">minutes</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span> <span class="n">seconds</span><span class="o">=</span><span class="mi">600</span><span class="p">)</span>  <span class="c1"># adds up to 365 days</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">year</span><span class="o">.</span><span class="n">total_seconds</span><span class="p">()</span>
<span class="go">31536000.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">year</span> <span class="o">==</span> <span class="n">another_year</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ten_years</span> <span class="o">=</span> <span class="mi">10</span> <span class="o">*</span> <span class="n">year</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ten_years</span><span class="p">,</span> <span class="n">ten_years</span><span class="o">.</span><span class="n">days</span> <span class="o">//</span> <span class="mi">365</span>
<span class="go">(datetime.timedelta(days=3650), 10)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nine_years</span> <span class="o">=</span> <span class="n">ten_years</span> <span class="o">-</span> <span class="n">year</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nine_years</span><span class="p">,</span> <span class="n">nine_years</span><span class="o">.</span><span class="n">days</span> <span class="o">//</span> <span class="mi">365</span>
<span class="go">(datetime.timedelta(days=3285), 9)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">three_years</span> <span class="o">=</span> <span class="n">nine_years</span> <span class="o">//</span> <span class="mi">3</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">three_years</span><span class="p">,</span> <span class="n">three_years</span><span class="o">.</span><span class="n">days</span> <span class="o">//</span> <span class="mi">365</span>
<span class="go">(datetime.timedelta(days=1095), 3)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">abs</span><span class="p">(</span><span class="n">three_years</span> <span class="o">-</span> <span class="n">ten_years</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">three_years</span> <span class="o">+</span> <span class="n">year</span>
<span class="go">True</span>
</pre></div>
</div>
</div>
<div class="section" id="date-objects">
<span id="datetime-date"></span><h2><a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 对象<a class="headerlink" href="#date-objects" title="永久链接至标题">¶</a></h2>
<p><a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 对象代表一个理想化历法中的日期（年、月和日），即当今的格列高利历向前后两个方向无限延伸。公元 1  年 1 月 1 日是第 1 日，公元 1 年 1 月 2 日是第 2 日， 依此类推。 这与 Dershowitz 与 Reingold 所著 Calendrical Calculations 中“预期格列高利”历法的定义一致，它是适用于该书中所有运算的基础历法。 请参阅该书了解在预期格列高利历序列与许多其他历法系统之间进行转换的算法。</p>
<dl class="class">
<dt id="datetime.date">
<em class="property">class </em><code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">date</code><span class="sig-paren">(</span><em class="sig-param">year</em>, <em class="sig-param">month</em>, <em class="sig-param">day</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date" title="永久链接至目标">¶</a></dt>
<dd><p>所有参数都是必要的。 参数必须是在下面范围内的整数：</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">MINYEAR</span> <span class="pre">&lt;=</span> <span class="pre">year</span> <span class="pre">&lt;=</span> <span class="pre">MAXYEAR</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre">&lt;=</span> <span class="pre">month</span> <span class="pre">&lt;=</span> <span class="pre">12</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre">&lt;=</span> <span class="pre">日期</span> <span class="pre">&lt;=</span> <span class="pre">给定年月对应的天数</span></code></p></li>
</ul>
<p>如果参数不在这些范围内，则抛出 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> 异常。</p>
</dd></dl>

<p>其它构造器，所有的类方法：</p>
<dl class="method">
<dt id="datetime.date.today">
<em class="property">classmethod </em><code class="sig-prename descclassname">date.</code><code class="sig-name descname">today</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.today" title="永久链接至目标">¶</a></dt>
<dd><p>返回当地的当前日期。与``date.fromtimestamp(time.time())``等价。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.date.fromtimestamp">
<em class="property">classmethod </em><code class="sig-prename descclassname">date.</code><code class="sig-name descname">fromtimestamp</code><span class="sig-paren">(</span><em class="sig-param">timestamp</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.fromtimestamp" title="永久链接至目标">¶</a></dt>
<dd><p>返回对应于 POSIX 时间戳例如 <a class="reference internal" href="time.html#time.time" title="time.time"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.time()</span></code></a> 返回值的本地日期。 这可能引发 <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a>，如果时间戳数值超出所在平台 C <code class="xref c c-func docutils literal notranslate"><span class="pre">localtime()</span></code> 函数的支持范围的话，并会在 <code class="xref c c-func docutils literal notranslate"><span class="pre">localtime()</span></code> 出错时引发 <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>。 通常该数值会被限制在 1970 年至 2038 年之间。 请注意在时间戳概念包含闰秒的非 POSIX 系统上，闰秒会被 <a class="reference internal" href="#datetime.date.fromtimestamp" title="datetime.date.fromtimestamp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromtimestamp()</span></code></a> 所忽略。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.3 版更改: </span>引发 <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a> 而不是 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>，如果时间戳数值超出所在平台 C <code class="xref c c-func docutils literal notranslate"><span class="pre">localtime()</span></code> 函数的支持范围的话，并会在 <code class="xref c c-func docutils literal notranslate"><span class="pre">localtime()</span></code> 出错时引发 <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> 而不是 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="datetime.date.fromordinal">
<em class="property">classmethod </em><code class="sig-prename descclassname">date.</code><code class="sig-name descname">fromordinal</code><span class="sig-paren">(</span><em class="sig-param">ordinal</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.fromordinal" title="永久链接至目标">¶</a></dt>
<dd><p>返回对应于预期格列高利历序号的日期，其中公元 1 年 1 月 1 日的序号为 1。 除非 <code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre">&lt;=</span> <span class="pre">序号</span> <span class="pre">&lt;=</span> <span class="pre">date.max.toordinal()</span></code> 否则会引发 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>。 对于任意日期 <em>d</em>， <code class="docutils literal notranslate"><span class="pre">date.fromordinal(d.toordinal())</span> <span class="pre">==</span> <span class="pre">d</span></code>。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.date.fromisoformat">
<em class="property">classmethod </em><code class="sig-prename descclassname">date.</code><code class="sig-name descname">fromisoformat</code><span class="sig-paren">(</span><em class="sig-param">date_string</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.fromisoformat" title="永久链接至目标">¶</a></dt>
<dd><p>返回对应于 <a class="reference internal" href="#datetime.date.isoformat" title="datetime.date.isoformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">date.isoformat()</span></code></a> 所提供的 <em>date_string</em> 格式的 <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a>。 特别地，此函数支持 <code class="docutils literal notranslate"><span class="pre">YYYY-MM-DD</span></code> 格式的字符串。</p>
<div class="admonition caution">
<p class="admonition-title">警告</p>
<p>此函数不支持解析任意 ISO 8601 字符串 —— 它的目的只是作为 <a class="reference internal" href="#datetime.date.isoformat" title="datetime.date.isoformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">date.isoformat()</span></code></a> 的逆操作。</p>
</div>
<div class="versionadded">
<p><span class="versionmodified added">3.7 新版功能.</span></p>
</div>
</dd></dl>

<p>类属性：</p>
<dl class="attribute">
<dt id="datetime.date.min">
<code class="sig-prename descclassname">date.</code><code class="sig-name descname">min</code><a class="headerlink" href="#datetime.date.min" title="永久链接至目标">¶</a></dt>
<dd><p>最小的日期 <code class="docutils literal notranslate"><span class="pre">date(MINYEAR,</span> <span class="pre">1,</span> <span class="pre">1)</span></code> 。</p>
</dd></dl>

<dl class="attribute">
<dt id="datetime.date.max">
<code class="sig-prename descclassname">date.</code><code class="sig-name descname">max</code><a class="headerlink" href="#datetime.date.max" title="永久链接至目标">¶</a></dt>
<dd><p>最大的日期 ，<code class="docutils literal notranslate"><span class="pre">date(MAXYEAR,</span> <span class="pre">12,</span> <span class="pre">31)</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="datetime.date.resolution">
<code class="sig-prename descclassname">date.</code><code class="sig-name descname">resolution</code><a class="headerlink" href="#datetime.date.resolution" title="永久链接至目标">¶</a></dt>
<dd><p>两个日期对象的最小间隔，<code class="docutils literal notranslate"><span class="pre">timedelta(days=1)</span></code>。</p>
</dd></dl>

<p>实例属性（只读）：</p>
<dl class="attribute">
<dt id="datetime.date.year">
<code class="sig-prename descclassname">date.</code><code class="sig-name descname">year</code><a class="headerlink" href="#datetime.date.year" title="永久链接至目标">¶</a></dt>
<dd><p>在 <a class="reference internal" href="#datetime.MINYEAR" title="datetime.MINYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MINYEAR</span></code></a> 和 <a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MAXYEAR</span></code></a> 之间，包含边界。</p>
</dd></dl>

<dl class="attribute">
<dt id="datetime.date.month">
<code class="sig-prename descclassname">date.</code><code class="sig-name descname">month</code><a class="headerlink" href="#datetime.date.month" title="永久链接至目标">¶</a></dt>
<dd><p>1 至 12（含）</p>
</dd></dl>

<dl class="attribute">
<dt id="datetime.date.day">
<code class="sig-prename descclassname">date.</code><code class="sig-name descname">day</code><a class="headerlink" href="#datetime.date.day" title="永久链接至目标">¶</a></dt>
<dd><p>返回1到指定年月的天数间的数字。</p>
</dd></dl>

<p>支持的运算：</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 40%" />
<col style="width: 60%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>运算</p></th>
<th class="head"><p>结果：</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">date2</span> <span class="pre">=</span> <span class="pre">date1</span> <span class="pre">+</span> <span class="pre">timedelta</span></code></p></td>
<td><p><em>date2</em> 等于从 <em>date1</em> 减去 <code class="docutils literal notranslate"><span class="pre">timedelta.days</span></code> 天。 (1)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">date2</span> <span class="pre">=</span> <span class="pre">date1</span> <span class="pre">-</span> <span class="pre">timedelta</span></code></p></td>
<td><p>计算 <em>date2</em> 的值使得 <code class="docutils literal notranslate"><span class="pre">date2</span> <span class="pre">+</span> <span class="pre">timedelta</span> <span class="pre">==</span> <span class="pre">date1</span></code>。 (2)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">timedelta</span> <span class="pre">=</span> <span class="pre">date1</span> <span class="pre">-</span> <span class="pre">date2</span></code></p></td>
<td><p>(3)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">date1</span> <span class="pre">&lt;</span> <span class="pre">date2</span></code></p></td>
<td><p>如果 <em>date1</em> 的时间在 <em>date2</em> 之前则认为 <em>date1</em> 小于 <em>date2</em> 。 (4)</p></td>
</tr>
</tbody>
</table>
<p>注释:</p>
<ol class="arabic simple">
<li><p>如果 <code class="docutils literal notranslate"><span class="pre">timedelta.days</span> <span class="pre">&gt;</span> <span class="pre">0</span></code> 则 <em>date2</em> 在时间线上前进，如果 <code class="docutils literal notranslate"><span class="pre">timedelta.days</span> <span class="pre">&lt;</span> <span class="pre">0</span></code> 则后退。 操作完成后 <code class="docutils literal notranslate"><span class="pre">date2</span> <span class="pre">-</span> <span class="pre">date1</span> <span class="pre">==</span> <span class="pre">timedelta.days</span></code>。 <code class="docutils literal notranslate"><span class="pre">timedelta.seconds</span></code> 和 <code class="docutils literal notranslate"><span class="pre">timedelta.microseconds</span></code> 会被忽略。如果 <code class="docutils literal notranslate"><span class="pre">date2.year</span></code> 将小于 <a class="reference internal" href="#datetime.MINYEAR" title="datetime.MINYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MINYEAR</span></code></a> 或大于 <a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MAXYEAR</span></code></a> 则会引发 <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a>。.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">timedelta.seconds</span></code> 和 <code class="docutils literal notranslate"><span class="pre">timedelta.microseconds</span></code> 会被忽略。</p></li>
<li><p>精确且不会溢出。 操作完成后 timedelta.seconds 和 timedelta.microseconds 均为0， 并且 date2 + timedelta == date1。</p></li>
<li><p>换句话说，当且仅当 <code class="docutils literal notranslate"><span class="pre">date1.toordinal()</span> <span class="pre">&lt;</span> <span class="pre">date2.toordinal()</span></code> 时 <code class="docutils literal notranslate"><span class="pre">date1</span> <span class="pre">&lt;</span> <span class="pre">date2</span></code>。 日期比较会引发 <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>，如果比较目标不为 <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 对象的话。 不过也可能会返回  <code class="docutils literal notranslate"><span class="pre">NotImplemented</span></code>，如果比较目标具有 <code class="xref py py-meth docutils literal notranslate"><span class="pre">timetuple()</span></code> 属性的话。 这个钩子给予其他日期对象类型实现混合类型比较的机会。 否则，当 <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 对象与不同类型的对象比较时将会引发 <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>，除非 <code class="docutils literal notranslate"><span class="pre">==</span></code> 或 <code class="docutils literal notranslate"><span class="pre">!=</span></code> 比较。 后两种情况将分别返回 <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a> 或 <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a>。</p></li>
</ol>
<p>日期可以作为字典的关键字。在布尔上下文中，所有的 <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 对象都视为真。</p>
<p>实例方法：</p>
<dl class="method">
<dt id="datetime.date.replace">
<code class="sig-prename descclassname">date.</code><code class="sig-name descname">replace</code><span class="sig-paren">(</span><em class="sig-param">year=self.year</em>, <em class="sig-param">month=self.month</em>, <em class="sig-param">day=self.day</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.replace" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个具有同样值的日期，除非通过任何关键字参数给出了某些形参的新值。 例如，如果 <code class="docutils literal notranslate"><span class="pre">d</span> <span class="pre">==</span> <span class="pre">date(2002,</span> <span class="pre">12,</span> <span class="pre">31)</span></code>，则 <code class="docutils literal notranslate"><span class="pre">d.replace(day=26)</span> <span class="pre">==</span> <span class="pre">date(2002,</span> <span class="pre">12,</span> <span class="pre">26)</span></code>。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.date.timetuple">
<code class="sig-prename descclassname">date.</code><code class="sig-name descname">timetuple</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.timetuple" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个 <a class="reference internal" href="time.html#time.struct_time" title="time.struct_time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time.struct_time</span></code></a>，即与 <a class="reference internal" href="time.html#time.localtime" title="time.localtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.localtime()</span></code></a> 的返回类型相同。 hours, minutes 和 seconds 值为 0, 且 DST 标志值为 -1。 <code class="docutils literal notranslate"><span class="pre">d.timetuple()</span></code> 等价于 <code class="docutils literal notranslate"><span class="pre">time.struct_time((d.year,</span> <span class="pre">d.month,</span> <span class="pre">d.day,</span> <span class="pre">0,</span> <span class="pre">0,</span> <span class="pre">0,</span> <span class="pre">d.weekday(),</span> <span class="pre">yday,</span> <span class="pre">-1))</span></code>，其中 <code class="docutils literal notranslate"><span class="pre">yday</span> <span class="pre">=</span> <span class="pre">d.toordinal()</span> <span class="pre">-</span> <span class="pre">date(d.year,</span> <span class="pre">1,</span> <span class="pre">1).toordinal()</span> <span class="pre">+</span> <span class="pre">1</span></code> 是日期在当前年份中的序号，起始序号 <code class="docutils literal notranslate"><span class="pre">1</span></code> 表示 1 月 1 日。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.date.toordinal">
<code class="sig-prename descclassname">date.</code><code class="sig-name descname">toordinal</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.toordinal" title="永久链接至目标">¶</a></dt>
<dd><p>返回日期的预期格列高利历序号，其中公元 1 年 1 月 1 日的序号为 1。 对于任意 <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 对象 <em>d</em>，<code class="docutils literal notranslate"><span class="pre">date.fromordinal(d.toordinal())</span> <span class="pre">==</span> <span class="pre">d</span></code>。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.date.weekday">
<code class="sig-prename descclassname">date.</code><code class="sig-name descname">weekday</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.weekday" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个整数代表星期几，星期一为0，星期天为6。例如， <code class="docutils literal notranslate"><span class="pre">date(2002,</span> <span class="pre">12,</span> <span class="pre">4).weekday()</span> <span class="pre">==</span> <span class="pre">2</span></code>，表示的是星期三。参阅 <a class="reference internal" href="#datetime.date.isoweekday" title="datetime.date.isoweekday"><code class="xref py py-meth docutils literal notranslate"><span class="pre">isoweekday()</span></code></a>。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.date.isoweekday">
<code class="sig-prename descclassname">date.</code><code class="sig-name descname">isoweekday</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.isoweekday" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个整数代表星期几，星期一为1，星期天为7。例如：<code class="docutils literal notranslate"><span class="pre">date(2002,</span> <span class="pre">12,</span> <span class="pre">4).isoweekday()</span> <span class="pre">==</span> <span class="pre">3</span></code>,表示星期三。参见 <a class="reference internal" href="#datetime.date.weekday" title="datetime.date.weekday"><code class="xref py py-meth docutils literal notranslate"><span class="pre">weekday()</span></code></a>, <a class="reference internal" href="#datetime.date.isocalendar" title="datetime.date.isocalendar"><code class="xref py py-meth docutils literal notranslate"><span class="pre">isocalendar()</span></code></a>。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.date.isocalendar">
<code class="sig-prename descclassname">date.</code><code class="sig-name descname">isocalendar</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.isocalendar" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个三元元组，(ISO year, ISO week number, ISO weekday) 。</p>
<p>ISO日历是一个被广泛使用的公历。可以从 <a class="reference external" href="https://www.staff.science.uu.nl/~gent0113/calendar/isocalendar.htm">https://www.staff.science.uu.nl/~gent0113/calendar/isocalendar.htm</a> 上查看更完整的说明。</p>
<p>ISO 年由 52 或 53 个完整星期构成，每个星期开始于星期一结束于星期日。 一个 ISO 年的第一个星期就是（格列高利）历法的的一年中第一个包含星期四的星期。 这被称为 1 号星期，其中星期四所在的 ISO 年与其所在的格列高利年相同。</p>
<p>例如，2004 年的第一天是一个星期四，因此 ISO 2004 年的第一个星期开始于 2003 年 12 月 29 日星期一，结束于 2004 年 1 月 4 日星期日，因此 <code class="docutils literal notranslate"><span class="pre">date(2003,</span> <span class="pre">12,</span> <span class="pre">29).isocalendar()</span> <span class="pre">==</span> <span class="pre">(2004,</span> <span class="pre">1,</span> <span class="pre">1)</span></code> and <code class="docutils literal notranslate"><span class="pre">date(2004,</span> <span class="pre">1,</span> <span class="pre">4).isocalendar()</span> <span class="pre">==</span> <span class="pre">(2004,</span> <span class="pre">1,</span> <span class="pre">7)</span></code>。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.date.isoformat">
<code class="sig-prename descclassname">date.</code><code class="sig-name descname">isoformat</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.isoformat" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个 ISO 8601 格式的字符串， 'YYYY-MM-DD'。例如 <code class="docutils literal notranslate"><span class="pre">date(2002,</span> <span class="pre">12,</span> <span class="pre">4).isoformat()</span> <span class="pre">==</span> <span class="pre">'2002-12-04'</span></code>。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.date.__str__">
<code class="sig-prename descclassname">date.</code><code class="sig-name descname">__str__</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.__str__" title="永久链接至目标">¶</a></dt>
<dd><p>对于日期对象 <em>d</em>, <code class="docutils literal notranslate"><span class="pre">str(d)</span></code> 等价于 <code class="docutils literal notranslate"><span class="pre">d.isoformat()</span></code> 。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.date.ctime">
<code class="sig-prename descclassname">date.</code><code class="sig-name descname">ctime</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.ctime" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个代表日期的字符串，例如 <code class="docutils literal notranslate"><span class="pre">date(2002,</span> <span class="pre">12,</span> <span class="pre">4).ctime()</span> <span class="pre">==</span> <span class="pre">'Wed</span> <span class="pre">Dec</span> <span class="pre">4</span> <span class="pre">00:00:00</span> <span class="pre">2002'</span></code>。 在原生 C <code class="xref c c-func docutils literal notranslate"><span class="pre">ctime()</span></code> 函数 (<a class="reference internal" href="time.html#time.ctime" title="time.ctime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.ctime()</span></code></a> 会发起调用该函数，但 <a class="reference internal" href="#datetime.date.ctime" title="datetime.date.ctime"><code class="xref py py-meth docutils literal notranslate"><span class="pre">date.ctime()</span></code></a> 则不会) 遵循 C 标准的平台上，<code class="docutils literal notranslate"><span class="pre">d.ctime()</span></code> 等价于 <code class="docutils literal notranslate"><span class="pre">time.ctime(time.mktime(d.timetuple()))</span></code>。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.date.strftime">
<code class="sig-prename descclassname">date.</code><code class="sig-name descname">strftime</code><span class="sig-paren">(</span><em class="sig-param">format</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.strftime" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个由显式格式字符串所指明的代表日期的字符串。 表示时、分或秒的格式代码值将为 0。 要获取格式指令的完整列表请参阅 <a class="reference internal" href="#strftime-strptime-behavior"><span class="std std-ref">strftime() 和 strptime() 的行为</span></a>。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.date.__format__">
<code class="sig-prename descclassname">date.</code><code class="sig-name descname">__format__</code><span class="sig-paren">(</span><em class="sig-param">format</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.__format__" title="永久链接至目标">¶</a></dt>
<dd><p>与 <a class="reference internal" href="#datetime.date.strftime" title="datetime.date.strftime"><code class="xref py py-meth docutils literal notranslate"><span class="pre">date.strftime()</span></code></a> 相同。 此方法使得为 <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 对象指定以 <a class="reference internal" href="../reference/lexical_analysis.html#f-strings"><span class="std std-ref">格式化字符串字面值</span></a> 表示的格式化字符串以及使用 <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a> 进行格式化成为可能。 要获取格式指令的完整列表，请参阅 <a class="reference internal" href="#strftime-strptime-behavior"><span class="std std-ref">strftime() 和 strptime() 的行为</span></a>。</p>
</dd></dl>

<p>计算距离特定事件天数的例子:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">time</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">date</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">today</span> <span class="o">=</span> <span class="n">date</span><span class="o">.</span><span class="n">today</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">today</span>
<span class="go">datetime.date(2007, 12, 5)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">today</span> <span class="o">==</span> <span class="n">date</span><span class="o">.</span><span class="n">fromtimestamp</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">my_birthday</span> <span class="o">=</span> <span class="n">date</span><span class="p">(</span><span class="n">today</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">24</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">if</span> <span class="n">my_birthday</span> <span class="o">&lt;</span> <span class="n">today</span><span class="p">:</span>
<span class="gp">... </span>    <span class="n">my_birthday</span> <span class="o">=</span> <span class="n">my_birthday</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">year</span><span class="o">=</span><span class="n">today</span><span class="o">.</span><span class="n">year</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">my_birthday</span>
<span class="go">datetime.date(2008, 6, 24)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">time_to_birthday</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="n">my_birthday</span> <span class="o">-</span> <span class="n">today</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">time_to_birthday</span><span class="o">.</span><span class="n">days</span>
<span class="go">202</span>
</pre></div>
</div>
<p>使用 <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 的例子:</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">date</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">date</span><span class="o">.</span><span class="n">fromordinal</span><span class="p">(</span><span class="mi">730920</span><span class="p">)</span> <span class="c1"># 730920th day after 1. 1. 0001</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span>
<span class="go">datetime.date(2002, 3, 11)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">timetuple</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">t</span><span class="p">:</span>     
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="go">2002                # year</span>
<span class="go">3                   # month</span>
<span class="go">11                  # day</span>
<span class="go">0</span>
<span class="go">0</span>
<span class="go">0</span>
<span class="go">0                   # weekday (0 = Monday)</span>
<span class="go">70                  # 70th day in the year</span>
<span class="go">-1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ic</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">isocalendar</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">ic</span><span class="p">:</span>    
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="go">2002                # ISO year</span>
<span class="go">11                  # ISO week number</span>
<span class="go">1                   # ISO day number ( 1 = Monday )</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">isoformat</span><span class="p">()</span>
<span class="go">&#39;2002-03-11&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%d</span><span class="s2">/%m/%y&quot;</span><span class="p">)</span>
<span class="go">&#39;11/03/02&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">&quot;%A </span><span class="si">%d</span><span class="s2">. %B %Y&quot;</span><span class="p">)</span>
<span class="go">&#39;Monday 11. March 2002&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;The </span><span class="si">{1}</span><span class="s1"> is {0:</span><span class="si">%d</span><span class="s1">}, the </span><span class="si">{2}</span><span class="s1"> is {0:%B}.&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="s2">&quot;day&quot;</span><span class="p">,</span> <span class="s2">&quot;month&quot;</span><span class="p">)</span>
<span class="go">&#39;The day is 11, the month is March.&#39;</span>
</pre></div>
</div>
</div>
<div class="section" id="datetime-objects">
<span id="datetime-datetime"></span><h2><a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象<a class="headerlink" href="#datetime-objects" title="永久链接至标题">¶</a></h2>
<p><a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象是一个包含了来自 <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 对象和 <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> 对象所有信息的单一对象。 与 <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 对象一样，<a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 假定当今的格列高利历向前后两个方向无限延伸；与 time 对象一样，<a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 假定每一天恰好有 3600*24 秒。</p>
<p>构造器 ：</p>
<dl class="class">
<dt id="datetime.datetime">
<em class="property">class </em><code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">datetime</code><span class="sig-paren">(</span><em class="sig-param">year</em>, <em class="sig-param">month</em>, <em class="sig-param">day</em>, <em class="sig-param">hour=0</em>, <em class="sig-param">minute=0</em>, <em class="sig-param">second=0</em>, <em class="sig-param">microsecond=0</em>, <em class="sig-param">tzinfo=None</em>, <em class="sig-param">*</em>, <em class="sig-param">fold=0</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime" title="永久链接至目标">¶</a></dt>
<dd><p>year, month 和 day 参数是必要的。 <em>tzinfo</em> 可以是 <code class="docutils literal notranslate"><span class="pre">None</span></code> 或者一个 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 子类的实例。 其余的参数必须是在下面范围内的整数：</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">MINYEAR</span> <span class="pre">&lt;=</span> <span class="pre">year</span> <span class="pre">&lt;=</span> <span class="pre">MAXYEAR</span></code>,</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre">&lt;=</span> <span class="pre">month</span> <span class="pre">&lt;=</span> <span class="pre">12</span></code>,</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre">&lt;=</span> <span class="pre">day</span> <span class="pre">&lt;=</span> <span class="pre">指定年月的天数</span></code>,</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre">&lt;=</span> <span class="pre">hour</span> <span class="pre">&lt;</span> <span class="pre">24</span></code>,</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre">&lt;=</span> <span class="pre">minute</span> <span class="pre">&lt;</span> <span class="pre">60</span></code>,</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre">&lt;=</span> <span class="pre">second</span> <span class="pre">&lt;</span> <span class="pre">60</span></code>,</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre">&lt;=</span> <span class="pre">microsecond</span> <span class="pre">&lt;</span> <span class="pre">1000000</span></code>,</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">fold</span> <span class="pre">in</span> <span class="pre">[0,</span> <span class="pre">1]</span></code>.</p></li>
</ul>
<p>如果参数不在这些范围内，则抛出 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> 异常。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.6 新版功能: </span>增加了 <code class="docutils literal notranslate"><span class="pre">fold</span></code> 参数。</p>
</div>
</dd></dl>

<p>其它构造器，所有的类方法：</p>
<dl class="method">
<dt id="datetime.datetime.today">
<em class="property">classmethod </em><code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">today</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.today" title="永久链接至目标">¶</a></dt>
<dd><p>返回当前的本地 datetime，<a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 值为 <code class="docutils literal notranslate"><span class="pre">None</span></code>。 这等价于 <code class="docutils literal notranslate"><span class="pre">datetime.fromtimestamp(time.time())</span></code>。 另请参阅 <a class="reference internal" href="#datetime.datetime.now" title="datetime.datetime.now"><code class="xref py py-meth docutils literal notranslate"><span class="pre">now()</span></code></a>, <a class="reference internal" href="#datetime.datetime.fromtimestamp" title="datetime.datetime.fromtimestamp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromtimestamp()</span></code></a>。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.datetime.now">
<em class="property">classmethod </em><code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">now</code><span class="sig-paren">(</span><em class="sig-param">tz=None</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.now" title="永久链接至目标">¶</a></dt>
<dd><p>返回当前的本地 date 和 time。 如果可选参数 <em>tz</em> 为 <code class="docutils literal notranslate"><span class="pre">None</span></code> 或未指定，这就类似于 <a class="reference internal" href="#datetime.datetime.today" title="datetime.datetime.today"><code class="xref py py-meth docutils literal notranslate"><span class="pre">today()</span></code></a>，但该方法会在可能的情况下提供比通过 <a class="reference internal" href="time.html#time.time" title="time.time"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.time()</span></code></a> 时间戳所获时间值更高的精度（例如，在提供了 C <code class="xref c c-func docutils literal notranslate"><span class="pre">gettimeofday()</span></code> 函数的平台上就可能做到）。</p>
<p>如果 <em>tz</em> 不为 <code class="docutils literal notranslate"><span class="pre">None</span></code>，它必须是 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 的子类的一个实例，并且当前日期和时间将转换为 <em>tz</em> 时区的日期和时间。 在这种情况下结果等价于 <code class="docutils literal notranslate"><span class="pre">tz.fromutc(datetime.utcnow().replace(tzinfo=tz))</span></code>。 另请参阅 <a class="reference internal" href="#datetime.datetime.today" title="datetime.datetime.today"><code class="xref py py-meth docutils literal notranslate"><span class="pre">today()</span></code></a>, <a class="reference internal" href="#datetime.datetime.utcnow" title="datetime.datetime.utcnow"><code class="xref py py-meth docutils literal notranslate"><span class="pre">utcnow()</span></code></a>。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.datetime.utcnow">
<em class="property">classmethod </em><code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">utcnow</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.utcnow" title="永久链接至目标">¶</a></dt>
<dd><p>返回当前 UTC 日期和时间，其中 <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 为 <code class="docutils literal notranslate"><span class="pre">None</span></code>。 这类似于 <a class="reference internal" href="#datetime.datetime.now" title="datetime.datetime.now"><code class="xref py py-meth docutils literal notranslate"><span class="pre">now()</span></code></a>，但返回的当前 UTC 日期和时间是一个本机 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象。 自动感知的当前 UTC 日期时间可通过调用 <code class="docutils literal notranslate"><span class="pre">datetime.now(timezone.utc)</span></code> 来获得。 另请参阅 <a class="reference internal" href="#datetime.datetime.now" title="datetime.datetime.now"><code class="xref py py-meth docutils literal notranslate"><span class="pre">now()</span></code></a>。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.datetime.fromtimestamp">
<em class="property">classmethod </em><code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">fromtimestamp</code><span class="sig-paren">(</span><em class="sig-param">timestamp</em>, <em class="sig-param">tz=None</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.fromtimestamp" title="永久链接至目标">¶</a></dt>
<dd><p>返回对应于 POSIX 时间戳例如 <a class="reference internal" href="time.html#time.time" title="time.time"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.time()</span></code></a> 的返回值的本地日期和时间。 如果可选参数 <em>tz</em> 为 <code class="docutils literal notranslate"><span class="pre">None</span></code> 或未指定，时间戳会被转换为所在平台的本地日期和时间，返回的 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象将为天真型。</p>
<p>如果 <em>tz</em> 不为 <code class="docutils literal notranslate"><span class="pre">None</span></code>，它必须是 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 子类的一个实例，并且时间戳将被转换到 <em>tz</em> 指定的时区。 在这种情况下结果等价于 <code class="docutils literal notranslate"><span class="pre">tz.fromutc(datetime.utcfromtimestamp(timestamp).replace(tzinfo=tz))</span></code>。</p>
<p><a class="reference internal" href="#datetime.datetime.fromtimestamp" title="datetime.datetime.fromtimestamp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromtimestamp()</span></code></a> 可能引发 <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a>，如果时间戳数值超出所在平台 C <code class="xref c c-func docutils literal notranslate"><span class="pre">localtime()</span></code> 或 <code class="xref c c-func docutils literal notranslate"><span class="pre">gmtime()</span></code> 函数的支持范围的话，并会在 <code class="xref c c-func docutils literal notranslate"><span class="pre">localtime()</span></code> 或 <code class="xref c c-func docutils literal notranslate"><span class="pre">gmtime()</span></code> 出错时引发 <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>。 通常该数值会被限制在 1970 年至 2038 年之间。 请注意在时间戳概念包含闰秒的非 POSIX 系统上，闰秒会被 <a class="reference internal" href="#datetime.datetime.fromtimestamp" title="datetime.datetime.fromtimestamp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromtimestamp()</span></code></a> 所忽略，结果可能导致两个相差一秒的时间戳产生相同的 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象。 另请参阅 <a class="reference internal" href="#datetime.datetime.utcfromtimestamp" title="datetime.datetime.utcfromtimestamp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">utcfromtimestamp()</span></code></a>。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.3 版更改: </span>引发 <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a> 而不是 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>，如果时间戳数值超出所在平台 C <code class="xref c c-func docutils literal notranslate"><span class="pre">localtime()</span></code> 或 <code class="xref c c-func docutils literal notranslate"><span class="pre">gmtime()</span></code> 函数的支持范围的话。 并会在 <code class="xref c c-func docutils literal notranslate"><span class="pre">localtime()</span></code> 或 <code class="xref c c-func docutils literal notranslate"><span class="pre">gmtime()</span></code> 出错时引发 <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> 而不是 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.6 版更改: </span><a class="reference internal" href="#datetime.datetime.fromtimestamp" title="datetime.datetime.fromtimestamp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromtimestamp()</span></code></a> 可能返回 <a class="reference internal" href="#datetime.datetime.fold" title="datetime.datetime.fold"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fold</span></code></a> 值设为 1 的实例。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="datetime.datetime.utcfromtimestamp">
<em class="property">classmethod </em><code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">utcfromtimestamp</code><span class="sig-paren">(</span><em class="sig-param">timestamp</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.utcfromtimestamp" title="永久链接至目标">¶</a></dt>
<dd><p>返回对应于 POSIX 时间戳的 UTC <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>，其中 <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 为 <code class="docutils literal notranslate"><span class="pre">None</span></code>。 这可能引发 <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a>，如果时间戳数值超出所在平台 C <code class="xref c c-func docutils literal notranslate"><span class="pre">gmtime()</span></code> 函数的支持范围的话，并会在 <code class="xref c c-func docutils literal notranslate"><span class="pre">gmtime()</span></code> 出错时引发 <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>。 通常该数值并会限制在 1970 年至 2038 年之间。</p>
<p>要得到一个感知型 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象，应调用 <a class="reference internal" href="#datetime.datetime.fromtimestamp" title="datetime.datetime.fromtimestamp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromtimestamp()</span></code></a>:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">datetime</span><span class="o">.</span><span class="n">fromtimestamp</span><span class="p">(</span><span class="n">timestamp</span><span class="p">,</span> <span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
</pre></div>
</div>
<p>在 POSIX 兼容的平台上，它等价于以下表达式:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">datetime</span><span class="p">(</span><span class="mi">1970</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span> <span class="o">+</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">seconds</span><span class="o">=</span><span class="n">timestamp</span><span class="p">)</span>
</pre></div>
</div>
<p>不同之处在于后一种形式总是支持完整年份范围：从 <a class="reference internal" href="#datetime.MINYEAR" title="datetime.MINYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MINYEAR</span></code></a> 到 <a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MAXYEAR</span></code></a> 的开区间。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.3 版更改: </span>引发 <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a> 而不是 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>，如果时间戳数值超出所在平台 C <code class="xref c c-func docutils literal notranslate"><span class="pre">gmtime()</span></code> 函数的支持范围的话。 并会在 <code class="xref c c-func docutils literal notranslate"><span class="pre">gmtime()</span></code> 出错时引发 <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> 而不是 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="datetime.datetime.fromordinal">
<em class="property">classmethod </em><code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">fromordinal</code><span class="sig-paren">(</span><em class="sig-param">ordinal</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.fromordinal" title="永久链接至目标">¶</a></dt>
<dd><p>返回对应于预期格列高利历序号的 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>，其中公元 1 年 1 月 1 日的序号为 1。 除非 <code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre">&lt;=</span> <span class="pre">ordinal</span> <span class="pre">&lt;=</span> <span class="pre">datetime.max.toordinal()</span></code> 否则会引发 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>。 结果的hour, minute, second 和 microsecond 值均为 0，并且 <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 值为 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.datetime.combine">
<em class="property">classmethod </em><code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">combine</code><span class="sig-paren">(</span><em class="sig-param">date</em>, <em class="sig-param">time</em>, <em class="sig-param">tzinfo=self.tzinfo</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.combine" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个新的 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象，对象的日期数值等于给定的 <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 对象的数值，时间数值等于给定的 <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> 对象的数值。 如果提供 <em>tzinfo</em> 参数，其值会被用来设置结果的 <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 属性，否则将会使用 <em>time</em> 参数的 <a class="reference internal" href="#datetime.time.tzinfo" title="datetime.time.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 属性。</p>
<p>对于任意 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象 <em>d</em>，<code class="docutils literal notranslate"><span class="pre">d</span> <span class="pre">==</span> <span class="pre">datetime.combine(d.date(),</span> <span class="pre">d.time(),</span> <span class="pre">d.tzinfo)</span></code>。 如果 date 是一个 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象，它的时间数值和 <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 属性会被忽略。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.6 版更改: </span>增加了 <em>tzinfo</em> 参数。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="datetime.datetime.fromisoformat">
<em class="property">classmethod </em><code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">fromisoformat</code><span class="sig-paren">(</span><em class="sig-param">date_string</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.fromisoformat" title="永久链接至目标">¶</a></dt>
<dd><p>返回对应于 <a class="reference internal" href="#datetime.date.isoformat" title="datetime.date.isoformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">date.isoformat()</span></code></a> 和 <a class="reference internal" href="#datetime.datetime.isoformat" title="datetime.datetime.isoformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">datetime.isoformat()</span></code></a> 所提供的格式字符串中 <em>date_string</em> 的 <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>。 特别地，此函数支持 <code class="docutils literal notranslate"><span class="pre">YYYY-MM-DD[*HH[:MM[:SS[.fff[fff]]]][+HH:MM[:SS[.ffffff]]]]</span></code> 格式的字符串，其中 <code class="docutils literal notranslate"><span class="pre">*</span></code> 可匹配任意单个字符。</p>
<div class="admonition caution">
<p class="admonition-title">警告</p>
<p>此函数并不支持解析任意 ISO 8601 字符串 —— 它的目的只是作为 <a class="reference internal" href="#datetime.datetime.isoformat" title="datetime.datetime.isoformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">datetime.isoformat()</span></code></a> 的逆操作。 在第三方包 <a class="reference external" href="https://dateutil.readthedocs.io/en/stable/parser.html#dateutil.parser.isoparse">dateutil</a> 中提供了一个更完善的 ISO 8601 解析器 <code class="docutils literal notranslate"><span class="pre">dateutil.parser.isoparse</span></code>。</p>
</div>
<div class="versionadded">
<p><span class="versionmodified added">3.7 新版功能.</span></p>
</div>
</dd></dl>

<dl class="method">
<dt id="datetime.datetime.strptime">
<em class="property">classmethod </em><code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">strptime</code><span class="sig-paren">(</span><em class="sig-param">date_string</em>, <em class="sig-param">format</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.strptime" title="永久链接至目标">¶</a></dt>
<dd><p>返回根据 <em>format</em> 解析与 <em>date_string</em> 相对应的 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>，这等价于 <code class="docutils literal notranslate"><span class="pre">datetime(*(time.strptime(date_string,</span> <span class="pre">format)[0:6]))</span></code>，如果 date_string 和 format 无法被 <a class="reference internal" href="time.html#time.strptime" title="time.strptime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strptime()</span></code></a> 解析或它返回一个不是时间元组的值，则将引发 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>。 要获取格式化指令的完整列表请参阅 <a class="reference internal" href="#strftime-strptime-behavior"><span class="std std-ref">strftime() 和 strptime() 的行为</span></a>。</p>
</dd></dl>

<p>类属性：</p>
<dl class="attribute">
<dt id="datetime.datetime.min">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">min</code><a class="headerlink" href="#datetime.datetime.min" title="永久链接至目标">¶</a></dt>
<dd><p>最早的可表示 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>，<code class="docutils literal notranslate"><span class="pre">datetime(MINYEAR,</span> <span class="pre">1,</span> <span class="pre">1,</span> <span class="pre">tzinfo=None)</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="datetime.datetime.max">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">max</code><a class="headerlink" href="#datetime.datetime.max" title="永久链接至目标">¶</a></dt>
<dd><p>最晚的可表示 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>，<code class="docutils literal notranslate"><span class="pre">datetime(MAXYEAR,</span> <span class="pre">12,</span> <span class="pre">31,</span> <span class="pre">23,</span> <span class="pre">59,</span> <span class="pre">59,</span> <span class="pre">999999,</span> <span class="pre">tzinfo=None)</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="datetime.datetime.resolution">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">resolution</code><a class="headerlink" href="#datetime.datetime.resolution" title="永久链接至目标">¶</a></dt>
<dd><p>两个不相等的 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象之间可能的最小间隔，<code class="docutils literal notranslate"><span class="pre">timedelta(microseconds=1)</span></code>。</p>
</dd></dl>

<p>实例属性（只读）：</p>
<dl class="attribute">
<dt id="datetime.datetime.year">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">year</code><a class="headerlink" href="#datetime.datetime.year" title="永久链接至目标">¶</a></dt>
<dd><p>在 <a class="reference internal" href="#datetime.MINYEAR" title="datetime.MINYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MINYEAR</span></code></a> 和 <a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MAXYEAR</span></code></a> 之间，包含边界。</p>
</dd></dl>

<dl class="attribute">
<dt id="datetime.datetime.month">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">month</code><a class="headerlink" href="#datetime.datetime.month" title="永久链接至目标">¶</a></dt>
<dd><p>1 至 12（含）</p>
</dd></dl>

<dl class="attribute">
<dt id="datetime.datetime.day">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">day</code><a class="headerlink" href="#datetime.datetime.day" title="永久链接至目标">¶</a></dt>
<dd><p>返回1到指定年月的天数间的数字。</p>
</dd></dl>

<dl class="attribute">
<dt id="datetime.datetime.hour">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">hour</code><a class="headerlink" href="#datetime.datetime.hour" title="永久链接至目标">¶</a></dt>
<dd><p>取值范围是 <code class="docutils literal notranslate"><span class="pre">range(24)</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="datetime.datetime.minute">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">minute</code><a class="headerlink" href="#datetime.datetime.minute" title="永久链接至目标">¶</a></dt>
<dd><p>取值范围是 <code class="docutils literal notranslate"><span class="pre">range(60)</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="datetime.datetime.second">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">second</code><a class="headerlink" href="#datetime.datetime.second" title="永久链接至目标">¶</a></dt>
<dd><p>取值范围是 <code class="docutils literal notranslate"><span class="pre">range(60)</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="datetime.datetime.microsecond">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">microsecond</code><a class="headerlink" href="#datetime.datetime.microsecond" title="永久链接至目标">¶</a></dt>
<dd><p>取值范围是 <code class="docutils literal notranslate"><span class="pre">range(1000000)</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="datetime.datetime.tzinfo">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">tzinfo</code><a class="headerlink" href="#datetime.datetime.tzinfo" title="永久链接至目标">¶</a></dt>
<dd><p>作为 <em>tzinfo</em> 参数被传给 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 构造器的对象，如果没有传入值则为 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="datetime.datetime.fold">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">fold</code><a class="headerlink" href="#datetime.datetime.fold" title="永久链接至目标">¶</a></dt>
<dd><p>取值范围是 <code class="docutils literal notranslate"><span class="pre">[0,</span> <span class="pre">1]</span></code>。 用于在重复的时间段中消除边界时间歧义。 （当夏令时结束时回调时钟或由于政治原因导致当前时区的 UTC 时差减少就会出现重复的时间段。） 取值 0 (1) 表示两个时刻早于（晚于）所代表的同一边界时间。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.6 新版功能.</span></p>
</div>
</dd></dl>

<p>支持的运算：</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 55%" />
<col style="width: 45%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>运算</p></th>
<th class="head"><p>结果：</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">datetime2</span> <span class="pre">=</span> <span class="pre">datetime1</span> <span class="pre">+</span> <span class="pre">timedelta</span></code></p></td>
<td><p>(1)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">datetime2</span> <span class="pre">=</span> <span class="pre">datetime1</span> <span class="pre">-</span> <span class="pre">timedelta</span></code></p></td>
<td><p>(2)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">timedelta</span> <span class="pre">=</span> <span class="pre">datetime1</span> <span class="pre">-</span> <span class="pre">datetime2</span></code></p></td>
<td><p>(3)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">datetime1</span> <span class="pre">&lt;</span> <span class="pre">datetime2</span></code></p></td>
<td><p>比较 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 与 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>。 (4)</p></td>
</tr>
</tbody>
</table>
<ol class="arabic">
<li><p>datetime2 是从中去掉的一段 timedelta 的结果，如果 <code class="docutils literal notranslate"><span class="pre">timedelta.days</span></code> &gt; 0 则是在时间线上前进，如果 <code class="docutils literal notranslate"><span class="pre">timedelta.days</span></code> &lt; 0 则后退。 结果具有与输入的 datetime 相同的 <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 属性，并且操作完成后 datetime2 - datetime1 == timedelta。 如果 datetime2.year 将小于 <a class="reference internal" href="#datetime.MINYEAR" title="datetime.MINYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MINYEAR</span></code></a> 或大于 <a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MAXYEAR</span></code></a> 则会引发 <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a>。 请注意即使输入的是一个感知型对象，该方法也不会进行时区调整。</p></li>
<li><p>计算 datetime2 使得 datetime2 + timedelta == datetime1。 与相加操作一样，结果具有与输入的 datetime 相同的 <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 属性，即使输入的是一个感知型对象，该方法也不会进行时区调整。</p></li>
<li><p>从一个 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 减去一个 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 仅对两个操作数均为简单型或均为感知型时有定义。 如果一个是感知型而另一个是简单型，则会引发 <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>。</p>
<p>如果两个操作数都是简单型，或都是感知型且具有相同的 <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 属性，<a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 属性会被忽略，结果是一个使得 <code class="docutils literal notranslate"><span class="pre">datetime2</span> <span class="pre">+</span> <span class="pre">t</span> <span class="pre">==</span> <span class="pre">datetime1</span></code> 的 <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 对象 <em>t</em>。 在此情况下不会进行时区调整。</p>
<p>如果两个操作数都是感知型且具有不同的 <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 属性，<code class="docutils literal notranslate"><span class="pre">a-b</span></code> 操作的行为就如同 <em>a</em> 和 <em>b</em> 被首先转换为简单型 UTC 日期时间。 结果将是 <code class="docutils literal notranslate"><span class="pre">(a.replace(tzinfo=None)</span> <span class="pre">-</span> <span class="pre">a.utcoffset())</span> <span class="pre">-</span> <span class="pre">(b.replace(tzinfo=None)</span> <span class="pre">-</span> <span class="pre">b.utcoffset())</span></code> 除非具体实现绝对不溢出。</p>
</li>
<li><p>当 <em>datetime1</em> 的时间在 <em>datetime2</em> 之前则认为 <em>datetime1</em> 小于 <em>datetime2</em>。</p>
<p>如果比较的一方是简单型而另一方是感知型，则如果尝试进行顺序比较将引发 <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>。 对于相等比较，简单型实例将永远不等于感知型实例。</p>
<p>如果两个比较方都是感知型，且具有相同的 <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 属性，相同的 <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 属性会被忽略并对基本日期时间值进行比较。 如果两个比较方都是感知型且具有不同的 <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 属性，两个比较方将首先通过减去它们的 UTC 差值（使用 <code class="docutils literal notranslate"><span class="pre">self.utcoffset()</span></code> 获取）来进行调整。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.3 版更改: </span>简单型和感知型 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 实例之间的相等比较不会引发 <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>。</p>
</div>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>为了防止比较操作回退为默认的对象地址比较方案，datetime 比较通常会引发 <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>，如果比较目标不同样为 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象的话。 不过也可能会返回 <code class="docutils literal notranslate"><span class="pre">NotImplemented</span></code> 如果比较目标具有 <code class="xref py py-meth docutils literal notranslate"><span class="pre">timetuple()</span></code> 属性的话。 这个钩子给予其他日期对象类型实现混合类型比较的机会。 否则，当 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象与不同类型的对象比较时将会引发 <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>，除非 <code class="docutils literal notranslate"><span class="pre">==</span></code> 或 <code class="docutils literal notranslate"><span class="pre">!=</span></code> 比较。 后两种情况将分别返回 <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a> 或 <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a>。</p>
</div>
</li>
</ol>
<p><a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象可以用作字典的键。 在布尔运算时，所有 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象都被视为真值。</p>
<p>实例方法：</p>
<dl class="method">
<dt id="datetime.datetime.date">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">date</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.date" title="永久链接至目标">¶</a></dt>
<dd><p>返回具有同样 year, month 和 day 值的 <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 对象。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.datetime.time">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">time</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.time" title="永久链接至目标">¶</a></dt>
<dd><p>返回具有同样 hour, minute, second, microsecond 和 fold 值的 <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> 对象。 <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 为 <code class="docutils literal notranslate"><span class="pre">None</span></code>。 另请参见方法 <a class="reference internal" href="#datetime.datetime.timetz" title="datetime.datetime.timetz"><code class="xref py py-meth docutils literal notranslate"><span class="pre">timetz()</span></code></a>。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.6 版更改: </span>fold 值会被复制给返回的 <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> 对象。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="datetime.datetime.timetz">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">timetz</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.timetz" title="永久链接至目标">¶</a></dt>
<dd><p>返回具有同样 hour, minute, second, microsecond, fold 和 tzinfo 属性值的 <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> 对象。 另请参见方法 <a class="reference internal" href="time.html#module-time" title="time: Time access and conversions."><code class="xref py py-meth docutils literal notranslate"><span class="pre">time()</span></code></a>。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.6 版更改: </span>fold 值会被复制给返回的 <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> 对象。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="datetime.datetime.replace">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">replace</code><span class="sig-paren">(</span><em class="sig-param">year=self.year</em>, <em class="sig-param">month=self.month</em>, <em class="sig-param">day=self.day</em>, <em class="sig-param">hour=self.hour</em>, <em class="sig-param">minute=self.minute</em>, <em class="sig-param">second=self.second</em>, <em class="sig-param">microsecond=self.microsecond</em>, <em class="sig-param">tzinfo=self.tzinfo</em>, <em class="sig-param">* fold=0</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.replace" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个具有同样属性值的 datetime，除非通过任何关键字参数指定了某些属性值。 请注意可以通过指定 <code class="docutils literal notranslate"><span class="pre">tzinfo=None</span></code> 从一个感知型 datetime 创建一个简单型 datetime 而不必转换日期和时间值。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.6 新版功能: </span>增加了 <code class="docutils literal notranslate"><span class="pre">fold</span></code> 参数。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="datetime.datetime.astimezone">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">astimezone</code><span class="sig-paren">(</span><em class="sig-param">tz=None</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.astimezone" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个具有新的 <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 属性 <em>tz</em> 的 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象，并会调整日期和时间数据使得结果对应的 UTC 时间与 <em>self</em> 相同，但为 <em>tz</em> 时区的本地时间。</p>
<p><em>tz</em> 如果给出则必须是一个 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 子类的实例，并且其 <a class="reference internal" href="#datetime.datetime.utcoffset" title="datetime.datetime.utcoffset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">utcoffset()</span></code></a> 和 <a class="reference internal" href="#datetime.datetime.dst" title="datetime.datetime.dst"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dst()</span></code></a> 方法不可返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。 如果 <em>self</em> 为简单型，它会被假定为基于系统时区表示的时间。</p>
<p>如果调用时不传入参数（或传入 <code class="docutils literal notranslate"><span class="pre">tz=None</span></code>) 则将假定目标时区为系统的本地时区。 转换后 datetime 实例的 <code class="docutils literal notranslate"><span class="pre">.tzinfo</span></code> 属性将被设为一个 <a class="reference internal" href="#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a> 实例，时区名称和时差值将从系统获取。</p>
<p>如果 <code class="docutils literal notranslate"><span class="pre">self.tzinfo</span></code> 为 <em>tz</em>，<code class="docutils literal notranslate"><span class="pre">self.astimezone(tz)</span></code> 等于 <em>self</em>: 不会对日期或时间数据进行调整。 否则结果为 <em>tz</em> 时区的本地时间，代表的 UTC 时间与 <em>self</em> 相同：在 <code class="docutils literal notranslate"><span class="pre">astz</span> <span class="pre">=</span> <span class="pre">dt.astimezone(tz)</span></code> 之后，<code class="docutils literal notranslate"><span class="pre">astz</span> <span class="pre">-</span> <span class="pre">astz.utcoffset()</span></code> 将具有与 <code class="docutils literal notranslate"><span class="pre">dt</span> <span class="pre">-</span> <span class="pre">dt.utcoffset()</span></code> 相同的日期和时间数据。</p>
<p>如果你只想附加一个时区对象 <em>tz</em> 给一个 datetime 对象 <em>dt</em> 而不调整日期和时间数据，请使用 <code class="docutils literal notranslate"><span class="pre">dt.replace(tzinfo=tz)</span></code>。 如果你只想从一个感知型 datetime 对象 <em>dt</em> 移除时区对象则不转换日期和时间数据，请使用 <code class="docutils literal notranslate"><span class="pre">dt.replace(tzinfo=None)</span></code>。</p>
<p>请注意默认的 <a class="reference internal" href="#datetime.tzinfo.fromutc" title="datetime.tzinfo.fromutc"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tzinfo.fromutc()</span></code></a> 方法在 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 的子类中可以被重载，从而影响 <a class="reference internal" href="#datetime.datetime.astimezone" title="datetime.datetime.astimezone"><code class="xref py py-meth docutils literal notranslate"><span class="pre">astimezone()</span></code></a> 的返回结果。 如果忽略出错的情况，<a class="reference internal" href="#datetime.datetime.astimezone" title="datetime.datetime.astimezone"><code class="xref py py-meth docutils literal notranslate"><span class="pre">astimezone()</span></code></a> 的行为就类似于:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">astimezone</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tz</span><span class="p">):</span>
    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">tzinfo</span> <span class="ow">is</span> <span class="n">tz</span><span class="p">:</span>
        <span class="k">return</span> <span class="bp">self</span>
    <span class="c1"># Convert self to UTC, and attach the new time zone object.</span>
    <span class="n">utc</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">utcoffset</span><span class="p">())</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">tzinfo</span><span class="o">=</span><span class="n">tz</span><span class="p">)</span>
    <span class="c1"># Convert from UTC to tz&#39;s local time.</span>
    <span class="k">return</span> <span class="n">tz</span><span class="o">.</span><span class="n">fromutc</span><span class="p">(</span><span class="n">utc</span><span class="p">)</span>
</pre></div>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.3 版更改: </span><em>tz</em> 现在可以被省略。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.6 版更改: </span><a class="reference internal" href="#datetime.datetime.astimezone" title="datetime.datetime.astimezone"><code class="xref py py-meth docutils literal notranslate"><span class="pre">astimezone()</span></code></a> 方法可以由简单型实例调用，这将假定其表示本地时间。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="datetime.datetime.utcoffset">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">utcoffset</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.utcoffset" title="永久链接至目标">¶</a></dt>
<dd><p>如果 <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 为 <code class="docutils literal notranslate"><span class="pre">None</span></code>，则返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>，否则返回 <code class="docutils literal notranslate"><span class="pre">self.tzinfo.utcoffset(self)</span></code>，并且在后者不返回 <code class="docutils literal notranslate"><span class="pre">None</span></code> 或者一个幅度小于一天的 <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 对象时将引发异常。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.7 版更改: </span>UTC 时差不再限制为一个整数分钟值。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="datetime.datetime.dst">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">dst</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.dst" title="永久链接至目标">¶</a></dt>
<dd><p>如果 <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 为 <code class="docutils literal notranslate"><span class="pre">None</span></code>，则返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>，否则返回 <code class="docutils literal notranslate"><span class="pre">self.tzinfo.dst(self)</span></code>，并且在后者不返回 <code class="docutils literal notranslate"><span class="pre">None</span></code> 或者一个幅度小于一天的 <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 对象时将引发异常。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.7 版更改: </span>DST 差值不再限制为一个整数分钟值。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="datetime.datetime.tzname">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">tzname</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.tzname" title="永久链接至目标">¶</a></dt>
<dd><p>如果 <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 为 <code class="docutils literal notranslate"><span class="pre">None</span></code>，则返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>，否则返回 <code class="docutils literal notranslate"><span class="pre">self.tzinfo.tzname(self)</span></code>，如果后者不返回 <code class="docutils literal notranslate"><span class="pre">None</span></code> 或者一个字符串对象则将引发异常。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.datetime.timetuple">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">timetuple</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.timetuple" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个 <a class="reference internal" href="time.html#time.struct_time" title="time.struct_time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time.struct_time</span></code></a>，即与 <a class="reference internal" href="time.html#time.localtime" title="time.localtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.localtime()</span></code></a> 的返回类型相同。 <code class="docutils literal notranslate"><span class="pre">d.timetuple()</span></code> 等价于 <code class="docutils literal notranslate"><span class="pre">time.struct_time((d.year,</span> <span class="pre">d.month,</span> <span class="pre">d.day,</span> <span class="pre">d.hour,</span> <span class="pre">d.minute,</span> <span class="pre">d.second,</span> <span class="pre">d.weekday(),</span> <span class="pre">yday,</span> <span class="pre">dst))</span></code>，其中 <code class="docutils literal notranslate"><span class="pre">yday</span> <span class="pre">=</span> <span class="pre">d.toordinal()</span> <span class="pre">-</span> <span class="pre">date(d.year,</span> <span class="pre">1,</span> <span class="pre">1).toordinal()</span> <span class="pre">+</span> <span class="pre">1</span></code> 是日期在当前年份中的序号，起始序号 <code class="docutils literal notranslate"><span class="pre">1</span></code> 表示 1 月 1 日。 结果的 <code class="xref py py-attr docutils literal notranslate"><span class="pre">tm_isdst</span></code> 旗标的设定会依据 <a class="reference internal" href="#datetime.datetime.dst" title="datetime.datetime.dst"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dst()</span></code></a> 方法：如果 <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 为 <code class="docutils literal notranslate"><span class="pre">None</span></code> 或 <a class="reference internal" href="#datetime.datetime.dst" title="datetime.datetime.dst"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dst()</span></code></a> 返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>，则 <code class="xref py py-attr docutils literal notranslate"><span class="pre">tm_isdst</span></code> 将设为 <code class="docutils literal notranslate"><span class="pre">-1</span></code>；否则如果 <a class="reference internal" href="#datetime.datetime.dst" title="datetime.datetime.dst"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dst()</span></code></a> 返回一个非零值，则 <code class="xref py py-attr docutils literal notranslate"><span class="pre">tm_isdst</span></code> 将设为 <code class="docutils literal notranslate"><span class="pre">1</span></code>；否则 <code class="xref py py-attr docutils literal notranslate"><span class="pre">tm_isdst</span></code> 将设为 <code class="docutils literal notranslate"><span class="pre">0</span></code>。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.datetime.utctimetuple">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">utctimetuple</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.utctimetuple" title="永久链接至目标">¶</a></dt>
<dd><p>如果 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 实例 <em>d</em> 为简单型，这类似于 <code class="docutils literal notranslate"><span class="pre">d.timetuple()</span></code>，不同之处为 <code class="xref py py-attr docutils literal notranslate"><span class="pre">tm_isdst</span></code> 会强设为 0，无论 <code class="docutils literal notranslate"><span class="pre">d.dst()</span></code> 返回什么结果。 DST 对于 UTC 时间永远无效。</p>
<p>如果 <em>d</em> 为感知型，<em>d</em> 会通过减去 <code class="docutils literal notranslate"><span class="pre">d.utcoffset()</span></code> 被标准化为 UTC 时间，并返回标准化时间对应的 <a class="reference internal" href="time.html#time.struct_time" title="time.struct_time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time.struct_time</span></code></a>。 <code class="xref py py-attr docutils literal notranslate"><span class="pre">tm_isdst</span></code> 会强设为 0。 请注意如果 <em>d</em>.year 为 <code class="docutils literal notranslate"><span class="pre">MINYEAR</span></code> 或 <code class="docutils literal notranslate"><span class="pre">MAXYEAR</span></code> 并且 UTC 调整超出一年的边界则可能引发 <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a>。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.datetime.toordinal">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">toordinal</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.toordinal" title="永久链接至目标">¶</a></dt>
<dd><p>返回日期的预期格列高利历序号。 与 <code class="docutils literal notranslate"><span class="pre">self.date().toordinal()</span></code> 相同。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.datetime.timestamp">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">timestamp</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.timestamp" title="永久链接至目标">¶</a></dt>
<dd><p>返回对应于 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 实例的 POSIX 时间戳。 返回值是与 <a class="reference internal" href="time.html#time.time" title="time.time"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.time()</span></code></a> 类似的 <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a>。</p>
<p>简单型 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 实例会假定为代表本地时间，并且此方法依赖于平台的 C <code class="xref c c-func docutils literal notranslate"><span class="pre">mktime()</span></code> 函数来执行转换。 由于在许多平台上 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 支持的值范围比 <code class="xref c c-func docutils literal notranslate"><span class="pre">mktime()</span></code> 更广，对于极其遥远的过去或末来时间此方法可能引发 <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a>。</p>
<p>对于感知型 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 实例，返回值的计算方式为:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="n">dt</span> <span class="o">-</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">1970</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">))</span><span class="o">.</span><span class="n">total_seconds</span><span class="p">()</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified added">3.3 新版功能.</span></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.6 版更改: </span><a class="reference internal" href="#datetime.datetime.timestamp" title="datetime.datetime.timestamp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">timestamp()</span></code></a> 方法使用 <a class="reference internal" href="#datetime.datetime.fold" title="datetime.datetime.fold"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fold</span></code></a> 属性来消除重复间隔中的时间歧义。</p>
</div>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>没有一个方法能直接从简单型 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 实例获取 POSIX 时间戳来代表 UTC 时间。 如果你的应用使用此惯例方式并且你的系统时区不是设为 UTC，你可以通过提供 <code class="docutils literal notranslate"><span class="pre">tzinfo=timezone.utc</span></code> 来获取 POSIX 时间戳:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">timestamp</span> <span class="o">=</span> <span class="n">dt</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">tzinfo</span><span class="o">=</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span><span class="o">.</span><span class="n">timestamp</span><span class="p">()</span>
</pre></div>
</div>
<p>或者通过直接计算时间戳:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">timestamp</span> <span class="o">=</span> <span class="p">(</span><span class="n">dt</span> <span class="o">-</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">1970</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span> <span class="o">/</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">seconds</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
</div>
</dd></dl>

<dl class="method">
<dt id="datetime.datetime.weekday">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">weekday</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.weekday" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个整数代表星期几，星期一为 0，星期天为 6。 相当于 <code class="docutils literal notranslate"><span class="pre">self.date().weekday()</span></code>。 另请参阅 <a class="reference internal" href="#datetime.datetime.isoweekday" title="datetime.datetime.isoweekday"><code class="xref py py-meth docutils literal notranslate"><span class="pre">isoweekday()</span></code></a>。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.datetime.isoweekday">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">isoweekday</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.isoweekday" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个整数代表星期几，星期一为 1，星期天为 7。 相当于 <code class="docutils literal notranslate"><span class="pre">self.date().isoweekday()</span></code>。 另请参阅 <a class="reference internal" href="#datetime.datetime.weekday" title="datetime.datetime.weekday"><code class="xref py py-meth docutils literal notranslate"><span class="pre">weekday()</span></code></a>, <a class="reference internal" href="#datetime.datetime.isocalendar" title="datetime.datetime.isocalendar"><code class="xref py py-meth docutils literal notranslate"><span class="pre">isocalendar()</span></code></a>。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.datetime.isocalendar">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">isocalendar</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.isocalendar" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个 3 元组 (ISO 年份, ISO 周序号, ISO 周日期)。 相当于 <code class="docutils literal notranslate"><span class="pre">self.date().isocalendar()</span></code>。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.datetime.isoformat">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">isoformat</code><span class="sig-paren">(</span><em class="sig-param">sep='T'</em>, <em class="sig-param">timespec='auto'</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.isoformat" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个以 ISO 8601 格式表示日期和时间的字符串 YYYY-MM-DDTHH:MM:SS.ffffff，如果 <a class="reference internal" href="#datetime.datetime.microsecond" title="datetime.datetime.microsecond"><code class="xref py py-attr docutils literal notranslate"><span class="pre">microsecond</span></code></a> 为 0 则改用格式 YYYY-MM-DDTHH:MM:SS</p>
<p>如果 <a class="reference internal" href="#datetime.datetime.utcoffset" title="datetime.datetime.utcoffset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">utcoffset()</span></code></a> 不返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>，则会添加一个字符串来给出 UTC 时差: YYYY-MM-DDTHH:MM:SS.ffffff+HH:MM[:SS[.ffffff]]，如果 <a class="reference internal" href="#datetime.datetime.microsecond" title="datetime.datetime.microsecond"><code class="xref py py-attr docutils literal notranslate"><span class="pre">microsecond</span></code></a> 为 0 则改用格式 YYYY-MM-DDTHH:MM:SS+HH:MM[:SS[.ffffff]]。</p>
<p>可选参数 <em>sep</em> (默认为 <code class="docutils literal notranslate"><span class="pre">'T'</span></code>) 为单个分隔字符，会被放在结果的日期和时间两部分之间。例如</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">tzinfo</span><span class="p">,</span> <span class="n">timedelta</span><span class="p">,</span> <span class="n">datetime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">TZ</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">utcoffset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">minutes</span><span class="o">=-</span><span class="mi">399</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">datetime</span><span class="p">(</span><span class="mi">2002</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">25</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">TZ</span><span class="p">())</span><span class="o">.</span><span class="n">isoformat</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">)</span>
<span class="go">&#39;2002-12-25 00:00:00-06:39&#39;</span>
</pre></div>
</div>
<p>可选参数 <em>timespec</em> 要包含的额外时间组件值 (默认为 <code class="docutils literal notranslate"><span class="pre">'auto'</span></code>)。它可以是以下值之一：</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">'auto'</span></code>: 如果 <a class="reference internal" href="#datetime.datetime.microsecond" title="datetime.datetime.microsecond"><code class="xref py py-attr docutils literal notranslate"><span class="pre">microsecond</span></code></a> 为 0 则与 <code class="docutils literal notranslate"><span class="pre">'seconds'</span></code> 相同，否则与 <code class="docutils literal notranslate"><span class="pre">'microseconds'</span></code> 相同。</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">'hours'</span></code>: 以两个数码的 HH 格式包含 <a class="reference internal" href="#datetime.datetime.hour" title="datetime.datetime.hour"><code class="xref py py-attr docutils literal notranslate"><span class="pre">hour</span></code></a>。</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">'minutes'</span></code>: 以 HH:MM 格式包含 <a class="reference internal" href="#datetime.datetime.hour" title="datetime.datetime.hour"><code class="xref py py-attr docutils literal notranslate"><span class="pre">hour</span></code></a> 和 <a class="reference internal" href="#datetime.datetime.minute" title="datetime.datetime.minute"><code class="xref py py-attr docutils literal notranslate"><span class="pre">minute</span></code></a>。</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">'seconds'</span></code>: 以 HH:MM:SS 格式包含 <a class="reference internal" href="#datetime.datetime.hour" title="datetime.datetime.hour"><code class="xref py py-attr docutils literal notranslate"><span class="pre">hour</span></code></a>, <a class="reference internal" href="#datetime.datetime.minute" title="datetime.datetime.minute"><code class="xref py py-attr docutils literal notranslate"><span class="pre">minute</span></code></a> 和 <a class="reference internal" href="#datetime.datetime.second" title="datetime.datetime.second"><code class="xref py py-attr docutils literal notranslate"><span class="pre">second</span></code></a>。</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">'milliseconds'</span></code>: 包含完整时间，但将秒值的小数部分截断至微秒。 格式为 HH:MM:SS.sss。</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">'microseconds'</span></code>: 以HH:MM:SS.ffffff 格式包含完整时间。</p></li>
</ul>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>排除掉的时间部分将被截断，而不是被舍入。</p>
</div>
<p>对于无效的 <em>timespec</em> 参数将引发 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>。</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span><span class="o">.</span><span class="n">isoformat</span><span class="p">(</span><span class="n">timespec</span><span class="o">=</span><span class="s1">&#39;minutes&#39;</span><span class="p">)</span>   
<span class="go">&#39;2002-12-25T00:00&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2015</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="mi">59</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span><span class="o">.</span><span class="n">isoformat</span><span class="p">(</span><span class="n">timespec</span><span class="o">=</span><span class="s1">&#39;microseconds&#39;</span><span class="p">)</span>
<span class="go">&#39;2015-01-01T12:30:59.000000&#39;</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified added">3.6 新版功能: </span>增加了 <em>timespec</em> 参数。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="datetime.datetime.__str__">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">__str__</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.__str__" title="永久链接至目标">¶</a></dt>
<dd><p>对于 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 实例 <em>d</em>，<code class="docutils literal notranslate"><span class="pre">str(d)</span></code> 等价于 <code class="docutils literal notranslate"><span class="pre">d.isoformat('</span> <span class="pre">')</span></code>。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.datetime.ctime">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">ctime</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.ctime" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个代表日期和时间的字符串，例如 <code class="docutils literal notranslate"><span class="pre">datetime(2002,</span> <span class="pre">12,</span> <span class="pre">4,</span> <span class="pre">20,</span> <span class="pre">30,</span> <span class="pre">40).ctime()</span> <span class="pre">==</span> <span class="pre">'Wed</span> <span class="pre">Dec</span>&#160; <span class="pre">4</span> <span class="pre">20:30:40</span> <span class="pre">2002'</span></code>。 在原生 C <code class="xref c c-func docutils literal notranslate"><span class="pre">ctime()</span></code> 函数 (<a class="reference internal" href="time.html#time.ctime" title="time.ctime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.ctime()</span></code></a> 会发起调用该函数，但 <a class="reference internal" href="#datetime.datetime.ctime" title="datetime.datetime.ctime"><code class="xref py py-meth docutils literal notranslate"><span class="pre">datetime.ctime()</span></code></a> 则不会) 遵循 C 标准的平台上，<code class="docutils literal notranslate"><span class="pre">d.ctime()</span></code> 等价于 <code class="docutils literal notranslate"><span class="pre">time.ctime(time.mktime(d.timetuple()))</span></code>。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.datetime.strftime">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">strftime</code><span class="sig-paren">(</span><em class="sig-param">format</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.strftime" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个由显式格式字符串所指明的代表日期和时间的字符串， 要获取格式指令的完整列表请参阅 <a class="reference internal" href="#strftime-strptime-behavior"><span class="std std-ref">strftime() 和 strptime() 的行为</span></a>。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.datetime.__format__">
<code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">__format__</code><span class="sig-paren">(</span><em class="sig-param">format</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.__format__" title="永久链接至目标">¶</a></dt>
<dd><p>与 <a class="reference internal" href="#datetime.datetime.strftime" title="datetime.datetime.strftime"><code class="xref py py-meth docutils literal notranslate"><span class="pre">datetime.strftime()</span></code></a> 相同。 此方法使得为 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象指定以 <a class="reference internal" href="../reference/lexical_analysis.html#f-strings"><span class="std std-ref">格式化字符串字面值</span></a> 表示的格式化字符串以及使用 <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a> 进行格式化成为可能。 要获取格式指令的完整列表，请参阅 <a class="reference internal" href="#strftime-strptime-behavior"><span class="std std-ref">strftime() 和 strptime() 的行为</span></a>。</p>
</dd></dl>

<p>使用 datetime 对象的例子:</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span><span class="p">,</span> <span class="n">date</span><span class="p">,</span> <span class="n">time</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Using datetime.combine()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">date</span><span class="p">(</span><span class="mi">2005</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">14</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span> <span class="o">=</span> <span class="n">time</span><span class="p">(</span><span class="mi">12</span><span class="p">,</span> <span class="mi">30</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">datetime</span><span class="o">.</span><span class="n">combine</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">t</span><span class="p">)</span>
<span class="go">datetime.datetime(2005, 7, 14, 12, 30)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Using datetime.now() or datetime.utcnow()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>   
<span class="go">datetime.datetime(2007, 12, 6, 16, 29, 43, 79043)   # GMT +1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">datetime</span><span class="o">.</span><span class="n">utcnow</span><span class="p">()</span>   
<span class="go">datetime.datetime(2007, 12, 6, 15, 29, 43, 79060)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Using datetime.strptime()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">strptime</span><span class="p">(</span><span class="s2">&quot;21/11/06 16:30&quot;</span><span class="p">,</span> <span class="s2">&quot;</span><span class="si">%d</span><span class="s2">/%m/%y %H:%M&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span>
<span class="go">datetime.datetime(2006, 11, 21, 16, 30)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Using datetime.timetuple() to get tuple of all attributes</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tt</span> <span class="o">=</span> <span class="n">dt</span><span class="o">.</span><span class="n">timetuple</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">it</span> <span class="ow">in</span> <span class="n">tt</span><span class="p">:</span>   
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="n">it</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">2006    # year</span>
<span class="go">11      # month</span>
<span class="go">21      # day</span>
<span class="go">16      # hour</span>
<span class="go">30      # minute</span>
<span class="go">0       # second</span>
<span class="go">1       # weekday (0 = Monday)</span>
<span class="go">325     # number of days since 1st January</span>
<span class="go">-1      # dst - method tzinfo.dst() returned None</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Date in ISO format</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ic</span> <span class="o">=</span> <span class="n">dt</span><span class="o">.</span><span class="n">isocalendar</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">it</span> <span class="ow">in</span> <span class="n">ic</span><span class="p">:</span>   
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="n">it</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">2006    # ISO year</span>
<span class="go">47      # ISO week</span>
<span class="go">2       # ISO weekday</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Formatting datetime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">&quot;%A, </span><span class="si">%d</span><span class="s2">. %B %Y %I:%M%p&quot;</span><span class="p">)</span>
<span class="go">&#39;Tuesday, 21. November 2006 04:30PM&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;The </span><span class="si">{1}</span><span class="s1"> is {0:</span><span class="si">%d</span><span class="s1">}, the </span><span class="si">{2}</span><span class="s1"> is {0:%B}, the </span><span class="si">{3}</span><span class="s1"> is {0:%I:%M%p}.&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">dt</span><span class="p">,</span> <span class="s2">&quot;day&quot;</span><span class="p">,</span> <span class="s2">&quot;month&quot;</span><span class="p">,</span> <span class="s2">&quot;time&quot;</span><span class="p">)</span>
<span class="go">&#39;The day is 21, the month is November, the time is 04:30PM.&#39;</span>
</pre></div>
</div>
<p>使用 datetime 并附带 tzinfo:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">timedelta</span><span class="p">,</span> <span class="n">datetime</span><span class="p">,</span> <span class="n">tzinfo</span><span class="p">,</span> <span class="n">timezone</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">KabulTz</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span>
<span class="gp">... </span>    <span class="c1"># Kabul used +4 until 1945, when they moved to +4:30</span>
<span class="gp">... </span>    <span class="n">UTC_MOVE_DATE</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">1944</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">31</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">utcoffset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">if</span> <span class="n">dt</span><span class="o">.</span><span class="n">year</span> <span class="o">&lt;</span> <span class="mi">1945</span><span class="p">:</span>
<span class="gp">... </span>            <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
<span class="gp">... </span>        <span class="k">elif</span> <span class="p">(</span><span class="mi">1945</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="n">dt</span><span class="o">.</span><span class="n">timetuple</span><span class="p">()[:</span><span class="mi">5</span><span class="p">]</span> <span class="o">&lt;</span> <span class="p">(</span><span class="mi">1945</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">30</span><span class="p">):</span>
<span class="gp">... </span>            <span class="c1"># If dt falls in the imaginary range, use fold to decide how</span>
<span class="gp">... </span>            <span class="c1"># to resolve. See PEP495</span>
<span class="gp">... </span>            <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">minutes</span><span class="o">=</span><span class="p">(</span><span class="mi">30</span> <span class="k">if</span> <span class="n">dt</span><span class="o">.</span><span class="n">fold</span> <span class="k">else</span> <span class="mi">0</span><span class="p">))</span>
<span class="gp">... </span>        <span class="k">else</span><span class="p">:</span>
<span class="gp">... </span>            <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">minutes</span><span class="o">=</span><span class="mi">30</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">fromutc</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
<span class="gp">... </span>        <span class="c1"># A custom implementation is required for fromutc as</span>
<span class="gp">... </span>        <span class="c1"># the input to this function is a datetime with utc values</span>
<span class="gp">... </span>        <span class="c1"># but with a tzinfo set to self</span>
<span class="gp">... </span>        <span class="c1"># See datetime.astimezone or fromtimestamp</span>
<span class="gp">...</span>
<span class="gp">... </span>        <span class="c1"># Follow same validations as in datetime.tzinfo</span>
<span class="gp">... </span>        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dt</span><span class="p">,</span> <span class="n">datetime</span><span class="p">):</span>
<span class="gp">... </span>            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;fromutc() requires a datetime argument&quot;</span><span class="p">)</span>
<span class="gp">... </span>        <span class="k">if</span> <span class="n">dt</span><span class="o">.</span><span class="n">tzinfo</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">self</span><span class="p">:</span>
<span class="gp">... </span>            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;dt.tzinfo is not self&quot;</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">... </span>        <span class="k">if</span> <span class="n">dt</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">tzinfo</span><span class="o">=</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">UTC_MOVE_DATE</span><span class="p">:</span>
<span class="gp">... </span>            <span class="k">return</span> <span class="n">dt</span> <span class="o">+</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">minutes</span><span class="o">=</span><span class="mi">30</span><span class="p">)</span>
<span class="gp">... </span>        <span class="k">else</span><span class="p">:</span>
<span class="gp">... </span>            <span class="k">return</span> <span class="n">dt</span> <span class="o">+</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">tzname</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">if</span> <span class="n">dt</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">UTC_MOVE_DATE</span><span class="p">:</span>
<span class="gp">... </span>            <span class="k">return</span> <span class="s2">&quot;+04:30&quot;</span>
<span class="gp">... </span>        <span class="k">else</span><span class="p">:</span>
<span class="gp">... </span>            <span class="k">return</span> <span class="s2">&quot;+04&quot;</span>
<span class="gp">...</span>
<span class="gp">... </span>    <span class="k">def</span>  <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="si">}</span><span class="s2">()&quot;</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tz1</span> <span class="o">=</span> <span class="n">KabulTz</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Datetime before the change</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt1</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">1900</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">21</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">tz1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">dt1</span><span class="o">.</span><span class="n">utcoffset</span><span class="p">())</span>
<span class="go">4:00:00</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Datetime after the change</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt2</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2006</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">tz1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">dt2</span><span class="o">.</span><span class="n">utcoffset</span><span class="p">())</span>
<span class="go">4:30:00</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Convert datetime to another time zone</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt3</span> <span class="o">=</span> <span class="n">dt2</span><span class="o">.</span><span class="n">astimezone</span><span class="p">(</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt3</span>
<span class="go">datetime.datetime(2006, 6, 14, 8, 30, tzinfo=datetime.timezone.utc)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt2</span>
<span class="go">datetime.datetime(2006, 6, 14, 13, 0, tzinfo=KabulTz())</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt2</span><span class="o">.</span><span class="n">utctimetuple</span><span class="p">()</span> <span class="o">==</span> <span class="n">dt3</span><span class="o">.</span><span class="n">utctimetuple</span><span class="p">()</span>
<span class="go">True</span>
</pre></div>
</div>
</div>
<div class="section" id="time-objects">
<span id="datetime-time"></span><h2><a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> 对象<a class="headerlink" href="#time-objects" title="永久链接至标题">¶</a></h2>
<p>一个 time 对象代表某个日期内的（本地）时间，它独立于任何特定日期，并可通过 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 对象来调整。</p>
<dl class="class">
<dt id="datetime.time">
<em class="property">class </em><code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">time</code><span class="sig-paren">(</span><em class="sig-param">hour=0</em>, <em class="sig-param">minute=0</em>, <em class="sig-param">second=0</em>, <em class="sig-param">microsecond=0</em>, <em class="sig-param">tzinfo=None</em>, <em class="sig-param">*</em>, <em class="sig-param">fold=0</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.time" title="永久链接至目标">¶</a></dt>
<dd><p>所有参数都是可选的。 <em>tzinfo</em> 可以是 <code class="docutils literal notranslate"><span class="pre">None</span></code>，或者是一个 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 子类的实例。 其余的参数必须是在下面范围内的整数：</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre">&lt;=</span> <span class="pre">hour</span> <span class="pre">&lt;</span> <span class="pre">24</span></code>,</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre">&lt;=</span> <span class="pre">minute</span> <span class="pre">&lt;</span> <span class="pre">60</span></code>,</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre">&lt;=</span> <span class="pre">second</span> <span class="pre">&lt;</span> <span class="pre">60</span></code>,</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre">&lt;=</span> <span class="pre">microsecond</span> <span class="pre">&lt;</span> <span class="pre">1000000</span></code>,</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">fold</span> <span class="pre">in</span> <span class="pre">[0,</span> <span class="pre">1]</span></code>.</p></li>
</ul>
<p>如果给出一个此范围以外的参数，则会引发 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>。 所有参数值默认为 <code class="docutils literal notranslate"><span class="pre">0</span></code>，除了 <em>tzinfo</em> 默认为 <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>。</p>
</dd></dl>

<p>类属性：</p>
<dl class="attribute">
<dt id="datetime.time.min">
<code class="sig-prename descclassname">time.</code><code class="sig-name descname">min</code><a class="headerlink" href="#datetime.time.min" title="永久链接至目标">¶</a></dt>
<dd><p>早最的可表示 <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a>, <code class="docutils literal notranslate"><span class="pre">time(0,</span> <span class="pre">0,</span> <span class="pre">0,</span> <span class="pre">0)</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="datetime.time.max">
<code class="sig-prename descclassname">time.</code><code class="sig-name descname">max</code><a class="headerlink" href="#datetime.time.max" title="永久链接至目标">¶</a></dt>
<dd><p>最晚的可表示 <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a>, <code class="docutils literal notranslate"><span class="pre">time(23,</span> <span class="pre">59,</span> <span class="pre">59,</span> <span class="pre">999999)</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="datetime.time.resolution">
<code class="sig-prename descclassname">time.</code><code class="sig-name descname">resolution</code><a class="headerlink" href="#datetime.time.resolution" title="永久链接至目标">¶</a></dt>
<dd><p>两个不相等的 <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> 对象之间可能的最小间隔，<code class="docutils literal notranslate"><span class="pre">timedelta(microseconds=1)</span></code>，但是请注意 <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> 对象并不支持算术运算。</p>
</dd></dl>

<p>实例属性（只读）：</p>
<dl class="attribute">
<dt id="datetime.time.hour">
<code class="sig-prename descclassname">time.</code><code class="sig-name descname">hour</code><a class="headerlink" href="#datetime.time.hour" title="永久链接至目标">¶</a></dt>
<dd><p>取值范围是 <code class="docutils literal notranslate"><span class="pre">range(24)</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="datetime.time.minute">
<code class="sig-prename descclassname">time.</code><code class="sig-name descname">minute</code><a class="headerlink" href="#datetime.time.minute" title="永久链接至目标">¶</a></dt>
<dd><p>取值范围是 <code class="docutils literal notranslate"><span class="pre">range(60)</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="datetime.time.second">
<code class="sig-prename descclassname">time.</code><code class="sig-name descname">second</code><a class="headerlink" href="#datetime.time.second" title="永久链接至目标">¶</a></dt>
<dd><p>取值范围是 <code class="docutils literal notranslate"><span class="pre">range(60)</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="datetime.time.microsecond">
<code class="sig-prename descclassname">time.</code><code class="sig-name descname">microsecond</code><a class="headerlink" href="#datetime.time.microsecond" title="永久链接至目标">¶</a></dt>
<dd><p>取值范围是 <code class="docutils literal notranslate"><span class="pre">range(1000000)</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="datetime.time.tzinfo">
<code class="sig-prename descclassname">time.</code><code class="sig-name descname">tzinfo</code><a class="headerlink" href="#datetime.time.tzinfo" title="永久链接至目标">¶</a></dt>
<dd><p>作为 tzinfo 参数被传给 <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> 构造器的对象，如果没有传入值则为 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="datetime.time.fold">
<code class="sig-prename descclassname">time.</code><code class="sig-name descname">fold</code><a class="headerlink" href="#datetime.time.fold" title="永久链接至目标">¶</a></dt>
<dd><p>取值范围是 <code class="docutils literal notranslate"><span class="pre">[0,</span> <span class="pre">1]</span></code>。 用于在重复的时间段中消除边界时间歧义。 （当夏令时结束时回调时钟或由于政治原因导致当前时区的 UTC 时差减少就会出现重复的时间段。） 取值 0 (1) 表示两个时刻早于（晚于）所代表的同一边界时间。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.6 新版功能.</span></p>
</div>
</dd></dl>

<p>支持的运算：</p>
<ul>
<li><p>比较 <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> 和另一个 <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a>，当 <em>a</em> 的时间在 <em>b</em> 之前时，则认为 <em>a</em> 小于 <em>b</em>。 如果比较的一方是简单型而另一方是感知型，则如果尝试进行顺序比较将引发 <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>。 对于相等比较，简单型实例将永远不等于感知型实例。</p>
<p>如果两个比较方都是感知型，且具有相同的 <a class="reference internal" href="#datetime.time.tzinfo" title="datetime.time.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 属性，相同的 <a class="reference internal" href="#datetime.time.tzinfo" title="datetime.time.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 属性会被忽略并对基本时间值进行比较。 如果两个比较方都是感知型且具有不同的 <a class="reference internal" href="#datetime.time.tzinfo" title="datetime.time.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 属性，两个比较方将首先通过减去它们的 UTC 差值（使用 <code class="docutils literal notranslate"><span class="pre">self.utcoffset()</span></code> 获取）来进行调整。 为了防止将混合类型比较回退为基于对象地址的默认比较，当 <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> 对象与不同类型的对象比较时，将会引发 <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>，除非比较运算符是 <code class="docutils literal notranslate"><span class="pre">==</span></code> 或 <code class="docutils literal notranslate"><span class="pre">!=</span></code>。 在后一种情况下将分别返回 <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a> 或 <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a>。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.3 版更改: </span>简单型和感知型 <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> 实例之前的相等比较不会引发 <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>。</p>
</div>
</li>
<li><p>哈希，以便用作字典的键</p></li>
<li><p>高效的封存</p></li>
</ul>
<p>在布尔运算时，<a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> 对象总是被视为真值。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.5 版更改: </span>在 Python 3.5 之前，如果一个 <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> 对象代表 UTC 午夜零时则会被视为假值。 此行为被认为容易引发困惑和错误，因此从 Python 3.5 起已被去除。 详情参见 <a class="reference external" href="https://bugs.python.org/issue13936">bpo-13936</a>。</p>
</div>
<p>其他构造方法：</p>
<dl class="method">
<dt id="datetime.time.fromisoformat">
<em class="property">classmethod </em><code class="sig-prename descclassname">time.</code><code class="sig-name descname">fromisoformat</code><span class="sig-paren">(</span><em class="sig-param">time_string</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.time.fromisoformat" title="永久链接至目标">¶</a></dt>
<dd><p>返回对应于 <a class="reference internal" href="#datetime.time.isoformat" title="datetime.time.isoformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">time.isoformat()</span></code></a> 所提供的 <em>time_string</em> 格式的 <a class="reference internal" href="time.html#module-time" title="time: Time access and conversions."><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a>。 特别地此函数支持 <code class="docutils literal notranslate"><span class="pre">HH[:MM[:SS[.fff[fff]]]][+HH:MM[:SS[.ffffff]]]</span></code> 格式的字符串。</p>
<div class="admonition caution">
<p class="admonition-title">警告</p>
<p>此方法不支持解析任意 ISO 8601 字符串 —— 它的目的只是作为 <a class="reference internal" href="#datetime.time.isoformat" title="datetime.time.isoformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">time.isoformat()</span></code></a> 的逆操作。</p>
</div>
<div class="versionadded">
<p><span class="versionmodified added">3.7 新版功能.</span></p>
</div>
</dd></dl>

<p>实例方法：</p>
<dl class="method">
<dt id="datetime.time.replace">
<code class="sig-prename descclassname">time.</code><code class="sig-name descname">replace</code><span class="sig-paren">(</span><em class="sig-param">hour=self.hour</em>, <em class="sig-param">minute=self.minute</em>, <em class="sig-param">second=self.second</em>, <em class="sig-param">microsecond=self.microsecond</em>, <em class="sig-param">tzinfo=self.tzinfo</em>, <em class="sig-param">* fold=0</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.time.replace" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个具有同样属性值的 <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a>，除非通过任何关键字参数指定了某些属性值。 请注意可以通过指定 <code class="docutils literal notranslate"><span class="pre">tzinfo=None</span></code> 从一个感知型 <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> 创建一个简单型 <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> 而不必转换时间值。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.6 新版功能: </span>增加了 <code class="docutils literal notranslate"><span class="pre">fold</span></code> 参数。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="datetime.time.isoformat">
<code class="sig-prename descclassname">time.</code><code class="sig-name descname">isoformat</code><span class="sig-paren">(</span><em class="sig-param">timespec='auto'</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.time.isoformat" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个表示时间的字符串，使用 ISO 8601 格式 HH:MM:SS.ffffff，或者如果 <a class="reference internal" href="#datetime.time.microsecond" title="datetime.time.microsecond"><code class="xref py py-attr docutils literal notranslate"><span class="pre">microsecond</span></code></a> 为 0 则改用格式 HH:MM:SS， 如果 <a class="reference internal" href="#datetime.time.utcoffset" title="datetime.time.utcoffset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">utcoffset()</span></code></a> 不返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>，则会添加一个字符串来给出 UTC 时差: HH:MM:SS.ffffff+HH:MM[:SS[.ffffff]]，或者如果 self.microsecond 为 0 则改用格式 HH:MM:SS+HH:MM[:SS[.ffffff]]。</p>
<p>可选参数 <em>timespec</em> 要包含的额外时间组件值 (默认为 <code class="docutils literal notranslate"><span class="pre">'auto'</span></code>)。它可以是以下值之一：</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">'auto'</span></code>: 如果 <a class="reference internal" href="#datetime.time.microsecond" title="datetime.time.microsecond"><code class="xref py py-attr docutils literal notranslate"><span class="pre">microsecond</span></code></a> 为 0 则与 <code class="docutils literal notranslate"><span class="pre">'seconds'</span></code> 相同，否则与 <code class="docutils literal notranslate"><span class="pre">'microseconds'</span></code> 相同。</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">'hours'</span></code>: 以两个数码的 HH 格式包含 <a class="reference internal" href="#datetime.time.hour" title="datetime.time.hour"><code class="xref py py-attr docutils literal notranslate"><span class="pre">hour</span></code></a>。</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">'minutes'</span></code>: 以 HH:MM 格式包含 <a class="reference internal" href="#datetime.time.hour" title="datetime.time.hour"><code class="xref py py-attr docutils literal notranslate"><span class="pre">hour</span></code></a> 和 <a class="reference internal" href="#datetime.time.minute" title="datetime.time.minute"><code class="xref py py-attr docutils literal notranslate"><span class="pre">minute</span></code></a>。</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">'seconds'</span></code>: 以 HH:MM:SS 格式包含 <a class="reference internal" href="#datetime.time.hour" title="datetime.time.hour"><code class="xref py py-attr docutils literal notranslate"><span class="pre">hour</span></code></a>, <a class="reference internal" href="#datetime.time.minute" title="datetime.time.minute"><code class="xref py py-attr docutils literal notranslate"><span class="pre">minute</span></code></a> 和 <a class="reference internal" href="#datetime.time.second" title="datetime.time.second"><code class="xref py py-attr docutils literal notranslate"><span class="pre">second</span></code></a>。</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">'milliseconds'</span></code>: 包含完整时间，但将秒值的小数部分截断至微秒。 格式为 HH:MM:SS.sss。</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">'microseconds'</span></code>: 以HH:MM:SS.ffffff 格式包含完整时间。</p></li>
</ul>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>排除掉的时间部分将被截断，而不是被舍入。</p>
</div>
<p>对于无效的 <em>timespec</em> 参数将引发 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>。</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">time</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">time</span><span class="p">(</span><span class="n">hour</span><span class="o">=</span><span class="mi">12</span><span class="p">,</span> <span class="n">minute</span><span class="o">=</span><span class="mi">34</span><span class="p">,</span> <span class="n">second</span><span class="o">=</span><span class="mi">56</span><span class="p">,</span> <span class="n">microsecond</span><span class="o">=</span><span class="mi">123456</span><span class="p">)</span><span class="o">.</span><span class="n">isoformat</span><span class="p">(</span><span class="n">timespec</span><span class="o">=</span><span class="s1">&#39;minutes&#39;</span><span class="p">)</span>
<span class="go">&#39;12:34&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">time</span><span class="p">(</span><span class="n">hour</span><span class="o">=</span><span class="mi">12</span><span class="p">,</span> <span class="n">minute</span><span class="o">=</span><span class="mi">34</span><span class="p">,</span> <span class="n">second</span><span class="o">=</span><span class="mi">56</span><span class="p">,</span> <span class="n">microsecond</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span><span class="o">.</span><span class="n">isoformat</span><span class="p">(</span><span class="n">timespec</span><span class="o">=</span><span class="s1">&#39;microseconds&#39;</span><span class="p">)</span>
<span class="go">&#39;12:34:56.000000&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span><span class="o">.</span><span class="n">isoformat</span><span class="p">(</span><span class="n">timespec</span><span class="o">=</span><span class="s1">&#39;auto&#39;</span><span class="p">)</span>
<span class="go">&#39;12:34:56&#39;</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified added">3.6 新版功能: </span>增加了 <em>timespec</em> 参数。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="datetime.time.__str__">
<code class="sig-prename descclassname">time.</code><code class="sig-name descname">__str__</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.time.__str__" title="永久链接至目标">¶</a></dt>
<dd><p>对于时间对象 <em>t</em>, <code class="docutils literal notranslate"><span class="pre">str(t)</span></code> 等价于 <code class="docutils literal notranslate"><span class="pre">t.isoformat()</span></code>。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.time.strftime">
<code class="sig-prename descclassname">time.</code><code class="sig-name descname">strftime</code><span class="sig-paren">(</span><em class="sig-param">format</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.time.strftime" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个由显式格式字符串所指明的代表时间的字符串。 要获取格式指令的完整列表请参阅 <a class="reference internal" href="#strftime-strptime-behavior"><span class="std std-ref">strftime() 和 strptime() 的行为</span></a>。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.time.__format__">
<code class="sig-prename descclassname">time.</code><code class="sig-name descname">__format__</code><span class="sig-paren">(</span><em class="sig-param">format</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.time.__format__" title="永久链接至目标">¶</a></dt>
<dd><p>与 <a class="reference internal" href="#datetime.time.strftime" title="datetime.time.strftime"><code class="xref py py-meth docutils literal notranslate"><span class="pre">time.strftime()</span></code></a> 相同。 此方法使得为 <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> 对象指定以 <a class="reference internal" href="../reference/lexical_analysis.html#f-strings"><span class="std std-ref">格式化字符串字面值</span></a> 表示的格式化字符串以及使用 <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a> 进行格式化成为可能。 要获取格式指令的完整列表，请参阅 <a class="reference internal" href="#strftime-strptime-behavior"><span class="std std-ref">strftime() 和 strptime() 的行为</span></a>。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.time.utcoffset">
<code class="sig-prename descclassname">time.</code><code class="sig-name descname">utcoffset</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.time.utcoffset" title="永久链接至目标">¶</a></dt>
<dd><p>如果 <a class="reference internal" href="#datetime.time.tzinfo" title="datetime.time.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 为 <code class="docutils literal notranslate"><span class="pre">None</span></code>，则返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>，否则返回 <code class="docutils literal notranslate"><span class="pre">self.tzinfo.utcoffset(None)</span></code>，并且在后者不返回 <code class="docutils literal notranslate"><span class="pre">None</span></code> 或一个幅度小于一天的 a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 对象时将引发异常。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.7 版更改: </span>UTC 时差不再限制为一个整数分钟值。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="datetime.time.dst">
<code class="sig-prename descclassname">time.</code><code class="sig-name descname">dst</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.time.dst" title="永久链接至目标">¶</a></dt>
<dd><p>如果 <a class="reference internal" href="#datetime.time.tzinfo" title="datetime.time.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 为 <code class="docutils literal notranslate"><span class="pre">None</span></code>，则返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>，否则返回 <code class="docutils literal notranslate"><span class="pre">self.tzinfo.dst(None)</span></code>，并且在后者不返回 <code class="docutils literal notranslate"><span class="pre">None</span></code> 或者一个幅度小于一天的 <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 对象时将引发异常。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.7 版更改: </span>DST 差值不再限制为一个整数分钟值。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="datetime.time.tzname">
<code class="sig-prename descclassname">time.</code><code class="sig-name descname">tzname</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.time.tzname" title="永久链接至目标">¶</a></dt>
<dd><p>如果 <a class="reference internal" href="#datetime.time.tzinfo" title="datetime.time.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 为 <code class="docutils literal notranslate"><span class="pre">None</span></code>，则返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>，否则返回 <code class="docutils literal notranslate"><span class="pre">self.tzinfo.tzname(None)</span></code>，如果后者不返回 <code class="docutils literal notranslate"><span class="pre">None</span></code> 或者一个字符串对象则将引发异常。</p>
</dd></dl>

<p>示例:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">time</span><span class="p">,</span> <span class="n">tzinfo</span><span class="p">,</span> <span class="n">timedelta</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">TZ1</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">utcoffset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">tzname</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">dt</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="s2">&quot;+01:00&quot;</span>
<span class="gp">... </span>    <span class="k">def</span>  <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="si">}</span><span class="s2">()&quot;</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span> <span class="o">=</span> <span class="n">time</span><span class="p">(</span><span class="mi">12</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">TZ1</span><span class="p">())</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span>
<span class="go">datetime.time(12, 10, 30, tzinfo=TZ1())</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span><span class="o">.</span><span class="n">isoformat</span><span class="p">()</span>
<span class="go">&#39;12:10:30+01:00&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span><span class="o">.</span><span class="n">dst</span><span class="p">()</span>
<span class="go">datetime.timedelta(0)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span><span class="o">.</span><span class="n">tzname</span><span class="p">()</span>
<span class="go">&#39;+01:00&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">&quot;%H:%M:%S %Z&quot;</span><span class="p">)</span>
<span class="go">&#39;12:10:30 +01:00&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;The </span><span class="si">{}</span><span class="s1"> is {:%H:%M}.&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s2">&quot;time&quot;</span><span class="p">,</span> <span class="n">t</span><span class="p">)</span>
<span class="go">&#39;The time is 12:10.&#39;</span>
</pre></div>
</div>
</div>
<div class="section" id="tzinfo-objects">
<span id="datetime-tzinfo"></span><h2><a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 对象<a class="headerlink" href="#tzinfo-objects" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="datetime.tzinfo">
<em class="property">class </em><code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">tzinfo</code><a class="headerlink" href="#datetime.tzinfo" title="永久链接至目标">¶</a></dt>
<dd><p>这是一个抽象基类，即这个类不可直接被实例化。 你必须从该类派生一个实体子类，并且（至少）提供你使用 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 需要的标准 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 方法的实现。 <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code></a> 模块提供了 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 的一个简单实体子类，<a class="reference internal" href="#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a>，它能以与 UTC 的固定差值来表示不同的时区，例如 UTC 本身或北美的 EST 和 EDT。</p>
<p><a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 的（某个实体子类）的实例可以被传给 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 和 <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> 对象的构造器。 这些对象会将它们的属性视为对应于本地时间，并且 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 对象支持展示本地时间与 UTC 的差值、时区名称以及 DST 差值的方法，都是与传给它们的日期或时间对象的相对值。</p>
<p>对于封存操作的特殊要求：一个 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 子类必须具有可不带参数调用的 <a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code></a> 方法，否则它虽然可以被封存，但可能无法再次解封。 这是个技术性要求，在未来可能会被取消。</p>
<p>一个 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 的实体子类可能需要实现以下方法。 具体需要实现的方法取决于感知型 <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象如何使用它。 如果有疑问，可以简单地全都实现。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.tzinfo.utcoffset">
<code class="sig-prename descclassname">tzinfo.</code><code class="sig-name descname">utcoffset</code><span class="sig-paren">(</span><em class="sig-param">dt</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.tzinfo.utcoffset" title="永久链接至目标">¶</a></dt>
<dd><p>返回本地时间与 UTC 的差值为一个 <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 对象，在 UTC 以东为正值。 如果本地时间在 UTC 以西则为负值。 请注意这应当是与 UTC 的总计差值；即如果 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 对象同时代表时区和 DST 调整，<a class="reference internal" href="#datetime.tzinfo.utcoffset" title="datetime.tzinfo.utcoffset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">utcoffset()</span></code></a> 应当返回两者的和。 如果 UTC 差值不确定则返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。 其他情况下返回值必须为一个 <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 对象，值的范围严格限制于 <code class="docutils literal notranslate"><span class="pre">-timedelta(hours=24)</span></code> 和 <code class="docutils literal notranslate"><span class="pre">timedelta(hours=24)</span></code> 之间（差值的幅度必须小于一天）。 大多数 <a class="reference internal" href="#datetime.tzinfo.utcoffset" title="datetime.tzinfo.utcoffset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">utcoffset()</span></code></a> 的实现看起来可能像是以下两者之一:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">return</span> <span class="n">CONSTANT</span>                 <span class="c1"># fixed-offset class</span>
<span class="k">return</span> <span class="n">CONSTANT</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">dst</span><span class="p">(</span><span class="n">dt</span><span class="p">)</span>  <span class="c1"># daylight-aware class</span>
</pre></div>
</div>
<p>如果 <a class="reference internal" href="#datetime.tzinfo.utcoffset" title="datetime.tzinfo.utcoffset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">utcoffset()</span></code></a> 返回值不为 <code class="docutils literal notranslate"><span class="pre">None</span></code>，则 <a class="reference internal" href="#datetime.tzinfo.dst" title="datetime.tzinfo.dst"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dst()</span></code></a> 也不应返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
<p>默认的 <a class="reference internal" href="#datetime.tzinfo.utcoffset" title="datetime.tzinfo.utcoffset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">utcoffset()</span></code></a> 实现会引发 <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.7 版更改: </span>UTC 时差不再限制为一个整数分钟值。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="datetime.tzinfo.dst">
<code class="sig-prename descclassname">tzinfo.</code><code class="sig-name descname">dst</code><span class="sig-paren">(</span><em class="sig-param">dt</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.tzinfo.dst" title="永久链接至目标">¶</a></dt>
<dd><p>返回夏令时（DST）调整值为一个 <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 对象或当 DST 信息未知时返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。 如果 DST 未启用则返回 <code class="docutils literal notranslate"><span class="pre">timedelta(0)</span></code>。 如果 DST 已启用则返回调整值为一个 <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 对象（参见 <a class="reference internal" href="#datetime.tzinfo.utcoffset" title="datetime.tzinfo.utcoffset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">utcoffset()</span></code></a> 了解详情）。 请注意 DST 差值如果可用，就会直接被加入 <a class="reference internal" href="#datetime.tzinfo.utcoffset" title="datetime.tzinfo.utcoffset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">utcoffset()</span></code></a> 所返回的 UTC 差值，因此无需额外查询 <a class="reference internal" href="#datetime.tzinfo.dst" title="datetime.tzinfo.dst"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dst()</span></code></a> 除非你希望单独获取 DST 信息。 例如，<a class="reference internal" href="#datetime.datetime.timetuple" title="datetime.datetime.timetuple"><code class="xref py py-meth docutils literal notranslate"><span class="pre">datetime.timetuple()</span></code></a> 会调用其 <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 属性的 <a class="reference internal" href="#datetime.tzinfo.dst" title="datetime.tzinfo.dst"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dst()</span></code></a> 方法来确定应当如何设置 <code class="xref py py-attr docutils literal notranslate"><span class="pre">tm_isdst</span></code> 旗标，而 <a class="reference internal" href="#datetime.tzinfo.fromutc" title="datetime.tzinfo.fromutc"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tzinfo.fromutc()</span></code></a> 会调用 <a class="reference internal" href="#datetime.tzinfo.dst" title="datetime.tzinfo.dst"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dst()</span></code></a> 来在跨越时区时处理 DST 的改变。</p>
<p>一个可以同时处理标准时和夏令时的 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 子类的实例 <em>tz</em> 必须在此情形中保持一致：</p>
<p><code class="docutils literal notranslate"><span class="pre">tz.utcoffset(dt)</span> <span class="pre">-</span> <span class="pre">tz.dst(dt)</span></code></p>
<p>必须为具有同样的 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 子类实例 <code class="docutils literal notranslate"><span class="pre">dt.tzinfo</span> <span class="pre">==</span> <span class="pre">tz</span></code> 的每个 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象 <em>dt</em> 返回同样的结果，此表达式会产生时区的“标准时差”，它不应取决于具体日期或时间，只取决于地理位置。 <a class="reference internal" href="#datetime.datetime.astimezone" title="datetime.datetime.astimezone"><code class="xref py py-meth docutils literal notranslate"><span class="pre">datetime.astimezone()</span></code></a> 的实现依赖此方法，但无法检测违反规则的情况；确保符合规则是程序员的责任。 如果一个 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 子类不能保证这一点，也许应该重载 <a class="reference internal" href="#datetime.tzinfo.fromutc" title="datetime.tzinfo.fromutc"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tzinfo.fromutc()</span></code></a> 的默认实现以便在任何情况下与 <code class="xref py py-meth docutils literal notranslate"><span class="pre">astimezone()</span></code> 配合正常。</p>
<p>大多数 <a class="reference internal" href="#datetime.tzinfo.dst" title="datetime.tzinfo.dst"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dst()</span></code></a> 的实现可能会如以下两者之一:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
    <span class="c1"># a fixed-offset class:  doesn&#39;t account for DST</span>
    <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
</pre></div>
</div>
<p>或者</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
    <span class="c1"># Code to set dston and dstoff to the time zone&#39;s DST</span>
    <span class="c1"># transition times based on the input dt.year, and expressed</span>
    <span class="c1"># in standard local time.  Then</span>

    <span class="k">if</span> <span class="n">dston</span> <span class="o">&lt;=</span> <span class="n">dt</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">tzinfo</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">dstoff</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
</pre></div>
</div>
<p>默认的 <a class="reference internal" href="#datetime.tzinfo.dst" title="datetime.tzinfo.dst"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dst()</span></code></a> 实现会引发 <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.7 版更改: </span>DST 差值不再限制为一个整数分钟值。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="datetime.tzinfo.tzname">
<code class="sig-prename descclassname">tzinfo.</code><code class="sig-name descname">tzname</code><span class="sig-paren">(</span><em class="sig-param">dt</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.tzinfo.tzname" title="永久链接至目标">¶</a></dt>
<dd><p>将对应于 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象 <em>dt</em> 的时区名称作为字符串返回。 <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code></a> 模块没有定义任何字符串名称相关内容，也不要求名称有任何特定含义。 例如，&quot;GMT&quot;, &quot;UTC&quot;, &quot;-500&quot;, &quot;-5:00&quot;, &quot;EDT&quot;, &quot;US/Eastern&quot;, &quot;America/New York&quot; 都是有效的返回值。 如果字符串名称未知则返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。 请注意这是一个方法而不是一个固定的字符串，这主要是因为某些 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 子类可能需要根据所传入的特定 <em>dt</em> 值返回不同的名称，特别是当 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 类要负责处理夏令时的时候。</p>
<p>默认的 <a class="reference internal" href="#datetime.tzinfo.tzname" title="datetime.tzinfo.tzname"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tzname()</span></code></a> 实现会引发 <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>。</p>
</dd></dl>

<p>这些方法会被 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 或 <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> 对象调用，用来对应它们的同名方法。 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象会将自身作为传入参数，而 <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> 对象会将 <code class="docutils literal notranslate"><span class="pre">None</span></code> 作为传入参数。 这样 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 子类的方法应当准备好接受 <em>dt</em> 参数值为 <code class="docutils literal notranslate"><span class="pre">None</span></code> 或是 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 类的实例。</p>
<p>当传入 <code class="docutils literal notranslate"><span class="pre">None</span></code> 时，应当由类的设计者来决定最佳回应方式。 例如，返回 <code class="docutils literal notranslate"><span class="pre">None</span></code> 适用于希望该类提示时间对象不参与 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 协议处理。 让 <code class="docutils literal notranslate"><span class="pre">utcoffset(None)</span></code> 返回标准 UTC 时差也许会更有用处，如果没有其他用于发现标准时差的约定。</p>
<p>当传入一个 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象来回应 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 方法时，<code class="docutils literal notranslate"><span class="pre">dt.tzinfo</span></code> 与 <em>self</em> 是同一对象。 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 方法可以依赖这一点，除非用户代码直接调用了 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 方法。 此行为的目的是使得 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 方法将 <em>dt</em> 解读为本地时间，而不需要担心其他时区的相关对象。</p>
<p>还有一个额外的 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 方法，某个子类可能会希望重载它：</p>
<dl class="method">
<dt id="datetime.tzinfo.fromutc">
<code class="sig-prename descclassname">tzinfo.</code><code class="sig-name descname">fromutc</code><span class="sig-paren">(</span><em class="sig-param">dt</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.tzinfo.fromutc" title="永久链接至目标">¶</a></dt>
<dd><p>此方法会由默认的 <a class="reference internal" href="#datetime.datetime.astimezone" title="datetime.datetime.astimezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.astimezone()</span></code></a> 实现来调用。 当被调用时，<code class="docutils literal notranslate"><span class="pre">dt.tzinfo</span></code> 为 <em>self</em>，并且 <em>dt</em> 的日期和时间数据会被视为代表 UTC 时间。 <a class="reference internal" href="#datetime.tzinfo.fromutc" title="datetime.tzinfo.fromutc"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromutc()</span></code></a> 的目标是调整日期和时间数据，返回一个等价的 datetime 来表示 <em>self</em> 的本地时间。</p>
<p>大多数 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 子类应该能够毫无问题地继承默认的 <a class="reference internal" href="#datetime.tzinfo.fromutc" title="datetime.tzinfo.fromutc"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromutc()</span></code></a> 实现。 它的健壮性足以处理固定差值的时区以及同时负责标准时和夏令时的时区，对于后者甚至还能处理 DST 转换时间在各个年份有变化的情况。 一个默认 <a class="reference internal" href="#datetime.tzinfo.fromutc" title="datetime.tzinfo.fromutc"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromutc()</span></code></a> 实现可能无法在所有情况下正确处理的例子是（与 UTC 的）标准差值取决于所经过的特定日期和时间，这种情况可能由于政治原因而出现。 默认的 <code class="xref py py-meth docutils literal notranslate"><span class="pre">astimezone()</span></code> 和 <a class="reference internal" href="#datetime.tzinfo.fromutc" title="datetime.tzinfo.fromutc"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromutc()</span></code></a> 实现可能无法生成你希望的结果，如果这个结果恰好是跨越了标准差值发生改变的时刻当中的某个小时值的话。</p>
<p>忽略针对错误情况的代码，默认 <a class="reference internal" href="#datetime.tzinfo.fromutc" title="datetime.tzinfo.fromutc"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromutc()</span></code></a> 实现的行为方式如下:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">fromutc</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
    <span class="c1"># raise ValueError error if dt.tzinfo is not self</span>
    <span class="n">dtoff</span> <span class="o">=</span> <span class="n">dt</span><span class="o">.</span><span class="n">utcoffset</span><span class="p">()</span>
    <span class="n">dtdst</span> <span class="o">=</span> <span class="n">dt</span><span class="o">.</span><span class="n">dst</span><span class="p">()</span>
    <span class="c1"># raise ValueError if dtoff is None or dtdst is None</span>
    <span class="n">delta</span> <span class="o">=</span> <span class="n">dtoff</span> <span class="o">-</span> <span class="n">dtdst</span>  <span class="c1"># this is self&#39;s standard offset</span>
    <span class="k">if</span> <span class="n">delta</span><span class="p">:</span>
        <span class="n">dt</span> <span class="o">+=</span> <span class="n">delta</span>   <span class="c1"># convert to standard local time</span>
        <span class="n">dtdst</span> <span class="o">=</span> <span class="n">dt</span><span class="o">.</span><span class="n">dst</span><span class="p">()</span>
        <span class="c1"># raise ValueError if dtdst is None</span>
    <span class="k">if</span> <span class="n">dtdst</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">dt</span> <span class="o">+</span> <span class="n">dtdst</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">dt</span>
</pre></div>
</div>
</dd></dl>

<p>在以下 <a class="reference download internal" download="" href="../_downloads/6b45dc135219d1404be49d606589a11d/tzinfo_examples.py"><code class="xref download docutils literal notranslate"><span class="pre">tzinfo_examples.py</span></code></a> 文件中有一些 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 类的例子：</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">tzinfo</span><span class="p">,</span> <span class="n">timedelta</span><span class="p">,</span> <span class="n">datetime</span>

<span class="n">ZERO</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="n">HOUR</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="n">SECOND</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">seconds</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

<span class="c1"># A class capturing the platform&#39;s idea of local time.</span>
<span class="c1"># (May result in wrong values on historical times in</span>
<span class="c1">#  timezones where UTC offset and/or the DST rules had</span>
<span class="c1">#  changed in the past.)</span>
<span class="kn">import</span> <span class="nn">time</span> <span class="k">as</span> <span class="nn">_time</span>

<span class="n">STDOFFSET</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">seconds</span> <span class="o">=</span> <span class="o">-</span><span class="n">_time</span><span class="o">.</span><span class="n">timezone</span><span class="p">)</span>
<span class="k">if</span> <span class="n">_time</span><span class="o">.</span><span class="n">daylight</span><span class="p">:</span>
    <span class="n">DSTOFFSET</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">seconds</span> <span class="o">=</span> <span class="o">-</span><span class="n">_time</span><span class="o">.</span><span class="n">altzone</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
    <span class="n">DSTOFFSET</span> <span class="o">=</span> <span class="n">STDOFFSET</span>

<span class="n">DSTDIFF</span> <span class="o">=</span> <span class="n">DSTOFFSET</span> <span class="o">-</span> <span class="n">STDOFFSET</span>

<span class="k">class</span> <span class="nc">LocalTimezone</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">fromutc</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
        <span class="k">assert</span> <span class="n">dt</span><span class="o">.</span><span class="n">tzinfo</span> <span class="ow">is</span> <span class="bp">self</span>
        <span class="n">stamp</span> <span class="o">=</span> <span class="p">(</span><span class="n">dt</span> <span class="o">-</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">1970</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="bp">self</span><span class="p">))</span> <span class="o">//</span> <span class="n">SECOND</span>
        <span class="n">args</span> <span class="o">=</span> <span class="n">_time</span><span class="o">.</span><span class="n">localtime</span><span class="p">(</span><span class="n">stamp</span><span class="p">)[:</span><span class="mi">6</span><span class="p">]</span>
        <span class="n">dst_diff</span> <span class="o">=</span> <span class="n">DSTDIFF</span> <span class="o">//</span> <span class="n">SECOND</span>
        <span class="c1"># Detect fold</span>
        <span class="n">fold</span> <span class="o">=</span> <span class="p">(</span><span class="n">args</span> <span class="o">==</span> <span class="n">_time</span><span class="o">.</span><span class="n">localtime</span><span class="p">(</span><span class="n">stamp</span> <span class="o">-</span> <span class="n">dst_diff</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">datetime</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">microsecond</span><span class="o">=</span><span class="n">dt</span><span class="o">.</span><span class="n">microsecond</span><span class="p">,</span>
                        <span class="n">tzinfo</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span> <span class="n">fold</span><span class="o">=</span><span class="n">fold</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">utcoffset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_isdst</span><span class="p">(</span><span class="n">dt</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">DSTOFFSET</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">STDOFFSET</span>

    <span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_isdst</span><span class="p">(</span><span class="n">dt</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">DSTDIFF</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">ZERO</span>

    <span class="k">def</span> <span class="nf">tzname</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">_time</span><span class="o">.</span><span class="n">tzname</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_isdst</span><span class="p">(</span><span class="n">dt</span><span class="p">)]</span>

    <span class="k">def</span> <span class="nf">_isdst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
        <span class="n">tt</span> <span class="o">=</span> <span class="p">(</span><span class="n">dt</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="n">dt</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="n">dt</span><span class="o">.</span><span class="n">day</span><span class="p">,</span>
              <span class="n">dt</span><span class="o">.</span><span class="n">hour</span><span class="p">,</span> <span class="n">dt</span><span class="o">.</span><span class="n">minute</span><span class="p">,</span> <span class="n">dt</span><span class="o">.</span><span class="n">second</span><span class="p">,</span>
              <span class="n">dt</span><span class="o">.</span><span class="n">weekday</span><span class="p">(),</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
        <span class="n">stamp</span> <span class="o">=</span> <span class="n">_time</span><span class="o">.</span><span class="n">mktime</span><span class="p">(</span><span class="n">tt</span><span class="p">)</span>
        <span class="n">tt</span> <span class="o">=</span> <span class="n">_time</span><span class="o">.</span><span class="n">localtime</span><span class="p">(</span><span class="n">stamp</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">tt</span><span class="o">.</span><span class="n">tm_isdst</span> <span class="o">&gt;</span> <span class="mi">0</span>

<span class="n">Local</span> <span class="o">=</span> <span class="n">LocalTimezone</span><span class="p">()</span>


<span class="c1"># A complete implementation of current DST rules for major US time zones.</span>

<span class="k">def</span> <span class="nf">first_sunday_on_or_after</span><span class="p">(</span><span class="n">dt</span><span class="p">):</span>
    <span class="n">days_to_go</span> <span class="o">=</span> <span class="mi">6</span> <span class="o">-</span> <span class="n">dt</span><span class="o">.</span><span class="n">weekday</span><span class="p">()</span>
    <span class="k">if</span> <span class="n">days_to_go</span><span class="p">:</span>
        <span class="n">dt</span> <span class="o">+=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days_to_go</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">dt</span>


<span class="c1"># US DST Rules</span>
<span class="c1">#</span>
<span class="c1"># This is a simplified (i.e., wrong for a few cases) set of rules for US</span>
<span class="c1"># DST start and end times. For a complete and up-to-date set of DST rules</span>
<span class="c1"># and timezone definitions, visit the Olson Database (or try pytz):</span>
<span class="c1"># http://www.twinsun.com/tz/tz-link.htm</span>
<span class="c1"># http://sourceforge.net/projects/pytz/ (might not be up-to-date)</span>
<span class="c1">#</span>
<span class="c1"># In the US, since 2007, DST starts at 2am (standard time) on the second</span>
<span class="c1"># Sunday in March, which is the first Sunday on or after Mar 8.</span>
<span class="n">DSTSTART_2007</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="c1"># and ends at 2am (DST time) on the first Sunday of Nov.</span>
<span class="n">DSTEND_2007</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="c1"># From 1987 to 2006, DST used to start at 2am (standard time) on the first</span>
<span class="c1"># Sunday in April and to end at 2am (DST time) on the last</span>
<span class="c1"># Sunday of October, which is the first Sunday on or after Oct 25.</span>
<span class="n">DSTSTART_1987_2006</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="n">DSTEND_1987_2006</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">25</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="c1"># From 1967 to 1986, DST used to start at 2am (standard time) on the last</span>
<span class="c1"># Sunday in April (the one on or after April 24) and to end at 2am (DST time)</span>
<span class="c1"># on the last Sunday of October, which is the first Sunday</span>
<span class="c1"># on or after Oct 25.</span>
<span class="n">DSTSTART_1967_1986</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">24</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="n">DSTEND_1967_1986</span> <span class="o">=</span> <span class="n">DSTEND_1987_2006</span>

<span class="k">def</span> <span class="nf">us_dst_range</span><span class="p">(</span><span class="n">year</span><span class="p">):</span>
    <span class="c1"># Find start and end times for US DST. For years before 1967, return</span>
    <span class="c1"># start = end for no DST.</span>
    <span class="k">if</span> <span class="mi">2006</span> <span class="o">&lt;</span> <span class="n">year</span><span class="p">:</span>
        <span class="n">dststart</span><span class="p">,</span> <span class="n">dstend</span> <span class="o">=</span> <span class="n">DSTSTART_2007</span><span class="p">,</span> <span class="n">DSTEND_2007</span>
    <span class="k">elif</span> <span class="mi">1986</span> <span class="o">&lt;</span> <span class="n">year</span> <span class="o">&lt;</span> <span class="mi">2007</span><span class="p">:</span>
        <span class="n">dststart</span><span class="p">,</span> <span class="n">dstend</span> <span class="o">=</span> <span class="n">DSTSTART_1987_2006</span><span class="p">,</span> <span class="n">DSTEND_1987_2006</span>
    <span class="k">elif</span> <span class="mi">1966</span> <span class="o">&lt;</span> <span class="n">year</span> <span class="o">&lt;</span> <span class="mi">1987</span><span class="p">:</span>
        <span class="n">dststart</span><span class="p">,</span> <span class="n">dstend</span> <span class="o">=</span> <span class="n">DSTSTART_1967_1986</span><span class="p">,</span> <span class="n">DSTEND_1967_1986</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">datetime</span><span class="p">(</span><span class="n">year</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="p">)</span> <span class="o">*</span> <span class="mi">2</span>

    <span class="n">start</span> <span class="o">=</span> <span class="n">first_sunday_on_or_after</span><span class="p">(</span><span class="n">dststart</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">year</span><span class="o">=</span><span class="n">year</span><span class="p">))</span>
    <span class="n">end</span> <span class="o">=</span> <span class="n">first_sunday_on_or_after</span><span class="p">(</span><span class="n">dstend</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">year</span><span class="o">=</span><span class="n">year</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">start</span><span class="p">,</span> <span class="n">end</span>


<span class="k">class</span> <span class="nc">USTimeZone</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">hours</span><span class="p">,</span> <span class="n">reprname</span><span class="p">,</span> <span class="n">stdname</span><span class="p">,</span> <span class="n">dstname</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">stdoffset</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="n">hours</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">reprname</span> <span class="o">=</span> <span class="n">reprname</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">stdname</span> <span class="o">=</span> <span class="n">stdname</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dstname</span> <span class="o">=</span> <span class="n">dstname</span>

    <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">reprname</span>

    <span class="k">def</span> <span class="nf">tzname</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">dst</span><span class="p">(</span><span class="n">dt</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">dstname</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">stdname</span>

    <span class="k">def</span> <span class="nf">utcoffset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">stdoffset</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">dst</span><span class="p">(</span><span class="n">dt</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">dt</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">dt</span><span class="o">.</span><span class="n">tzinfo</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="c1"># An exception may be sensible here, in one or both cases.</span>
            <span class="c1"># It depends on how you want to treat them.  The default</span>
            <span class="c1"># fromutc() implementation (called by the default astimezone()</span>
            <span class="c1"># implementation) passes a datetime with dt.tzinfo is self.</span>
            <span class="k">return</span> <span class="n">ZERO</span>
        <span class="k">assert</span> <span class="n">dt</span><span class="o">.</span><span class="n">tzinfo</span> <span class="ow">is</span> <span class="bp">self</span>
        <span class="n">start</span><span class="p">,</span> <span class="n">end</span> <span class="o">=</span> <span class="n">us_dst_range</span><span class="p">(</span><span class="n">dt</span><span class="o">.</span><span class="n">year</span><span class="p">)</span>
        <span class="c1"># Can&#39;t compare naive to aware objects, so strip the timezone from</span>
        <span class="c1"># dt first.</span>
        <span class="n">dt</span> <span class="o">=</span> <span class="n">dt</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">tzinfo</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">start</span> <span class="o">+</span> <span class="n">HOUR</span> <span class="o">&lt;=</span> <span class="n">dt</span> <span class="o">&lt;</span> <span class="n">end</span> <span class="o">-</span> <span class="n">HOUR</span><span class="p">:</span>
            <span class="c1"># DST is in effect.</span>
            <span class="k">return</span> <span class="n">HOUR</span>
        <span class="k">if</span> <span class="n">end</span> <span class="o">-</span> <span class="n">HOUR</span> <span class="o">&lt;=</span> <span class="n">dt</span> <span class="o">&lt;</span> <span class="n">end</span><span class="p">:</span>
            <span class="c1"># Fold (an ambiguous hour): use dt.fold to disambiguate.</span>
            <span class="k">return</span> <span class="n">ZERO</span> <span class="k">if</span> <span class="n">dt</span><span class="o">.</span><span class="n">fold</span> <span class="k">else</span> <span class="n">HOUR</span>
        <span class="k">if</span> <span class="n">start</span> <span class="o">&lt;=</span> <span class="n">dt</span> <span class="o">&lt;</span> <span class="n">start</span> <span class="o">+</span> <span class="n">HOUR</span><span class="p">:</span>
            <span class="c1"># Gap (a non-existent hour): reverse the fold rule.</span>
            <span class="k">return</span> <span class="n">HOUR</span> <span class="k">if</span> <span class="n">dt</span><span class="o">.</span><span class="n">fold</span> <span class="k">else</span> <span class="n">ZERO</span>
        <span class="c1"># DST is off.</span>
        <span class="k">return</span> <span class="n">ZERO</span>

    <span class="k">def</span> <span class="nf">fromutc</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
        <span class="k">assert</span> <span class="n">dt</span><span class="o">.</span><span class="n">tzinfo</span> <span class="ow">is</span> <span class="bp">self</span>
        <span class="n">start</span><span class="p">,</span> <span class="n">end</span> <span class="o">=</span> <span class="n">us_dst_range</span><span class="p">(</span><span class="n">dt</span><span class="o">.</span><span class="n">year</span><span class="p">)</span>
        <span class="n">start</span> <span class="o">=</span> <span class="n">start</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">tzinfo</span><span class="o">=</span><span class="bp">self</span><span class="p">)</span>
        <span class="n">end</span> <span class="o">=</span> <span class="n">end</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">tzinfo</span><span class="o">=</span><span class="bp">self</span><span class="p">)</span>
        <span class="n">std_time</span> <span class="o">=</span> <span class="n">dt</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">stdoffset</span>
        <span class="n">dst_time</span> <span class="o">=</span> <span class="n">std_time</span> <span class="o">+</span> <span class="n">HOUR</span>
        <span class="k">if</span> <span class="n">end</span> <span class="o">&lt;=</span> <span class="n">dst_time</span> <span class="o">&lt;</span> <span class="n">end</span> <span class="o">+</span> <span class="n">HOUR</span><span class="p">:</span>
            <span class="c1"># Repeated hour</span>
            <span class="k">return</span> <span class="n">std_time</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">fold</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">std_time</span> <span class="o">&lt;</span> <span class="n">start</span> <span class="ow">or</span> <span class="n">dst_time</span> <span class="o">&gt;=</span> <span class="n">end</span><span class="p">:</span>
            <span class="c1"># Standard time</span>
            <span class="k">return</span> <span class="n">std_time</span>
        <span class="k">if</span> <span class="n">start</span> <span class="o">&lt;=</span> <span class="n">std_time</span> <span class="o">&lt;</span> <span class="n">end</span> <span class="o">-</span> <span class="n">HOUR</span><span class="p">:</span>
            <span class="c1"># Daylight saving time</span>
            <span class="k">return</span> <span class="n">dst_time</span>


<span class="n">Eastern</span>  <span class="o">=</span> <span class="n">USTimeZone</span><span class="p">(</span><span class="o">-</span><span class="mi">5</span><span class="p">,</span> <span class="s2">&quot;Eastern&quot;</span><span class="p">,</span>  <span class="s2">&quot;EST&quot;</span><span class="p">,</span> <span class="s2">&quot;EDT&quot;</span><span class="p">)</span>
<span class="n">Central</span>  <span class="o">=</span> <span class="n">USTimeZone</span><span class="p">(</span><span class="o">-</span><span class="mi">6</span><span class="p">,</span> <span class="s2">&quot;Central&quot;</span><span class="p">,</span>  <span class="s2">&quot;CST&quot;</span><span class="p">,</span> <span class="s2">&quot;CDT&quot;</span><span class="p">)</span>
<span class="n">Mountain</span> <span class="o">=</span> <span class="n">USTimeZone</span><span class="p">(</span><span class="o">-</span><span class="mi">7</span><span class="p">,</span> <span class="s2">&quot;Mountain&quot;</span><span class="p">,</span> <span class="s2">&quot;MST&quot;</span><span class="p">,</span> <span class="s2">&quot;MDT&quot;</span><span class="p">)</span>
<span class="n">Pacific</span>  <span class="o">=</span> <span class="n">USTimeZone</span><span class="p">(</span><span class="o">-</span><span class="mi">8</span><span class="p">,</span> <span class="s2">&quot;Pacific&quot;</span><span class="p">,</span>  <span class="s2">&quot;PST&quot;</span><span class="p">,</span> <span class="s2">&quot;PDT&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>请注意同时负责标准时和夏令时的 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 子类在每年两次的 DST 转换点上会出现不可避免的微妙问题。 具体而言，考虑美国东部时区 (UTC -0500)，它的 EDT 从三月的第二个星期天 1:59 (EST) 之后一分钟开始，并在十一月的第一个星期天 1:59 (EDT) 之后一分钟结束:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span>  <span class="n">UTC</span>   <span class="mi">3</span><span class="p">:</span><span class="n">MM</span>  <span class="mi">4</span><span class="p">:</span><span class="n">MM</span>  <span class="mi">5</span><span class="p">:</span><span class="n">MM</span>  <span class="mi">6</span><span class="p">:</span><span class="n">MM</span>  <span class="mi">7</span><span class="p">:</span><span class="n">MM</span>  <span class="mi">8</span><span class="p">:</span><span class="n">MM</span>
  <span class="n">EST</span>  <span class="mi">22</span><span class="p">:</span><span class="n">MM</span> <span class="mi">23</span><span class="p">:</span><span class="n">MM</span>  <span class="mi">0</span><span class="p">:</span><span class="n">MM</span>  <span class="mi">1</span><span class="p">:</span><span class="n">MM</span>  <span class="mi">2</span><span class="p">:</span><span class="n">MM</span>  <span class="mi">3</span><span class="p">:</span><span class="n">MM</span>
  <span class="n">EDT</span>  <span class="mi">23</span><span class="p">:</span><span class="n">MM</span>  <span class="mi">0</span><span class="p">:</span><span class="n">MM</span>  <span class="mi">1</span><span class="p">:</span><span class="n">MM</span>  <span class="mi">2</span><span class="p">:</span><span class="n">MM</span>  <span class="mi">3</span><span class="p">:</span><span class="n">MM</span>  <span class="mi">4</span><span class="p">:</span><span class="n">MM</span>

<span class="n">start</span>  <span class="mi">22</span><span class="p">:</span><span class="n">MM</span> <span class="mi">23</span><span class="p">:</span><span class="n">MM</span>  <span class="mi">0</span><span class="p">:</span><span class="n">MM</span>  <span class="mi">1</span><span class="p">:</span><span class="n">MM</span>  <span class="mi">3</span><span class="p">:</span><span class="n">MM</span>  <span class="mi">4</span><span class="p">:</span><span class="n">MM</span>

  <span class="n">end</span>  <span class="mi">23</span><span class="p">:</span><span class="n">MM</span>  <span class="mi">0</span><span class="p">:</span><span class="n">MM</span>  <span class="mi">1</span><span class="p">:</span><span class="n">MM</span>  <span class="mi">1</span><span class="p">:</span><span class="n">MM</span>  <span class="mi">2</span><span class="p">:</span><span class="n">MM</span>  <span class="mi">3</span><span class="p">:</span><span class="n">MM</span>
</pre></div>
</div>
<p>当 DST 开始时（见 &quot;start&quot; 行），本地时钟从 1:59 跳到 3:00。 形式为 2:MM 的时间值在那一天是没有意义的，因此在 DST 开始的那一天 <code class="docutils literal notranslate"><span class="pre">astimezone(Eastern)</span></code> 不会输出包含 <code class="docutils literal notranslate"><span class="pre">hour</span> <span class="pre">==</span> <span class="pre">2</span></code> 的结果。 例如，在 2016 年春季时钟向前调整时，我们得到</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span><span class="p">,</span> <span class="n">timezone</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">tzinfo_examples</span> <span class="kn">import</span> <span class="n">HOUR</span><span class="p">,</span> <span class="n">Eastern</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">u0</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2016</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">u</span> <span class="o">=</span> <span class="n">u0</span> <span class="o">+</span> <span class="n">i</span><span class="o">*</span><span class="n">HOUR</span>
<span class="gp">... </span>    <span class="n">t</span> <span class="o">=</span> <span class="n">u</span><span class="o">.</span><span class="n">astimezone</span><span class="p">(</span><span class="n">Eastern</span><span class="p">)</span>
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="n">u</span><span class="o">.</span><span class="n">time</span><span class="p">(),</span> <span class="s1">&#39;UTC =&#39;</span><span class="p">,</span> <span class="n">t</span><span class="o">.</span><span class="n">time</span><span class="p">(),</span> <span class="n">t</span><span class="o">.</span><span class="n">tzname</span><span class="p">())</span>
<span class="gp">...</span>
<span class="go">05:00:00 UTC = 00:00:00 EST</span>
<span class="go">06:00:00 UTC = 01:00:00 EST</span>
<span class="go">07:00:00 UTC = 03:00:00 EDT</span>
<span class="go">08:00:00 UTC = 04:00:00 EDT</span>
</pre></div>
</div>
<p>当 DST 结束时（见 &quot;end&quot; 行），会有更糟糕的潜在问题：本地时间值中有一个小时是不可能没有歧义的：夏令时的最后一小时。 即以美国东部时间表示当天夏令时结束时的形式为 5:MM UTC 的时间。 本地时间从 1:59 (夏令时) 再次跳回到 1:00 (标准时)。 形式为 1:MM 的本地时间就是有歧义的。 此时 <code class="xref py py-meth docutils literal notranslate"><span class="pre">astimezone()</span></code> 是通过将两个相邻的 UTC 小时映射到两个相同的本地小时来模仿本地时钟的行为。 在这个美国东部时间的示例中，形式为 5:MM 和 6:MM 的 UTC 时间在转换为美国东部时间时都将被映射到 1:MM，但前一个时间会将 <a class="reference internal" href="#datetime.datetime.fold" title="datetime.datetime.fold"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fold</span></code></a> 属性设为 0 而后一个时间会将其设为 1。 例如，在 2016 年秋季时钟往回调整时，我们得到</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">u0</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2016</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">u</span> <span class="o">=</span> <span class="n">u0</span> <span class="o">+</span> <span class="n">i</span><span class="o">*</span><span class="n">HOUR</span>
<span class="gp">... </span>    <span class="n">t</span> <span class="o">=</span> <span class="n">u</span><span class="o">.</span><span class="n">astimezone</span><span class="p">(</span><span class="n">Eastern</span><span class="p">)</span>
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="n">u</span><span class="o">.</span><span class="n">time</span><span class="p">(),</span> <span class="s1">&#39;UTC =&#39;</span><span class="p">,</span> <span class="n">t</span><span class="o">.</span><span class="n">time</span><span class="p">(),</span> <span class="n">t</span><span class="o">.</span><span class="n">tzname</span><span class="p">(),</span> <span class="n">t</span><span class="o">.</span><span class="n">fold</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">04:00:00 UTC = 00:00:00 EDT 0</span>
<span class="go">05:00:00 UTC = 01:00:00 EDT 0</span>
<span class="go">06:00:00 UTC = 01:00:00 EST 1</span>
<span class="go">07:00:00 UTC = 02:00:00 EST 0</span>
</pre></div>
</div>
<p>请注意不同的 <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 实例仅通过 <a class="reference internal" href="#datetime.datetime.fold" title="datetime.datetime.fold"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fold</span></code></a> 属性值来加以区分，它们在比较时会被视为相等。</p>
<p>不允许时间显示存在歧义的应用需要显式地检查 <a class="reference internal" href="#datetime.datetime.fold" title="datetime.datetime.fold"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fold</span></code></a> 属性的值，或者避免使用混合式的 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 子类；当使用 <a class="reference internal" href="#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a> 或者任何其他固定差值的 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 子类例如仅表示 EST（固定差值 -5 小时）或仅表示 EDT（固定差值 -4 小时）的类时是不会有歧义的。</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl>
<dt><a class="reference external" href="https://dateutil.readthedocs.io/en/stable/tz.html">dateutil.tz</a></dt><dd><p>该标准库具有 <a class="reference internal" href="#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a> 类用来将相对于 UTC 和 <a class="reference internal" href="#datetime.timezone.utc" title="datetime.timezone.utc"><code class="xref py py-attr docutils literal notranslate"><span class="pre">timezone.utc</span></code></a> 的任意固定差值处理为 UTC 时区实例。</p>
<p><em>dateutil.tz</em> 库将 <em>IANA 时区数据库</em> (又名 Olson 数据库) 引入 Python 并推荐使用。</p>
</dd>
<dt><a class="reference external" href="https://www.iana.org/time-zones">IANA 时区数据库</a></dt><dd><p>该时区数据库 (通常称为 tz, tzdata 或 zoneinfo) 包含大量代码和数据用来表示全球许多有代表性的地点的本地时间的历史信息。 它会定期进行更新以反映各政治实体对时区边界、UTC 差值和夏令时规则的更改。</p>
</dd>
</dl>
</div>
</div>
<div class="section" id="timezone-objects">
<span id="datetime-timezone"></span><h2><a class="reference internal" href="#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a> 对象<a class="headerlink" href="#timezone-objects" title="永久链接至标题">¶</a></h2>
<p><a class="reference internal" href="#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a> 类是 <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> 的一个子类，它的每个实例代表一个由与 UTC 的固定差值所定义的时区。 请注意该类的对象不可被用于代表某些特殊地点的时区信息，例如在一年的不同日期使用不同差值，或是在历史上对民用时间进行过调整的地点。</p>
<dl class="class">
<dt id="datetime.timezone">
<em class="property">class </em><code class="sig-prename descclassname">datetime.</code><code class="sig-name descname">timezone</code><span class="sig-paren">(</span><em class="sig-param">offset</em>, <em class="sig-param">name=None</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.timezone" title="永久链接至目标">¶</a></dt>
<dd><p><em>offset</em> 参数必须指定为一个 <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 对象，表示本地时间与 UTC 的差值。 它必须严格限制于 <code class="docutils literal notranslate"><span class="pre">-timedelta(hours=24)</span></code> 和 <code class="docutils literal notranslate"><span class="pre">timedelta(hours=24)</span></code> 之间，否则会引发 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>。</p>
<p><em>name</em> 参数是可选的。 如果指定则必须为一个字符串，它将被用作 <a class="reference internal" href="#datetime.datetime.tzname" title="datetime.datetime.tzname"><code class="xref py py-meth docutils literal notranslate"><span class="pre">datetime.tzname()</span></code></a> 方法的返回值。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.2 新版功能.</span></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.7 版更改: </span>UTC 时差不再限制为一个整数分钟值。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="datetime.timezone.utcoffset">
<code class="sig-prename descclassname">timezone.</code><code class="sig-name descname">utcoffset</code><span class="sig-paren">(</span><em class="sig-param">dt</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.timezone.utcoffset" title="永久链接至目标">¶</a></dt>
<dd><p>返回在构造 <a class="reference internal" href="#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a> 实例时指定的固定值。 <em>dt</em> 参数会被忽略。 返回值是一个 <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> 实例，其值等于本地时间与 UTC 之间的差值。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.7 版更改: </span>UTC 时差不再限制为一个整数分钟值。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="datetime.timezone.tzname">
<code class="sig-prename descclassname">timezone.</code><code class="sig-name descname">tzname</code><span class="sig-paren">(</span><em class="sig-param">dt</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.timezone.tzname" title="永久链接至目标">¶</a></dt>
<dd><p>返回在构造 <a class="reference internal" href="#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a> 实例时指定的固定值。 如果没有为构造器提供 <em>name</em>，则 <code class="docutils literal notranslate"><span class="pre">tzname(dt)</span></code> 所返回的名称将根据 <code class="docutils literal notranslate"><span class="pre">offset</span></code> 值按以下规则生成。 如果 <em>offset</em> 为 <code class="docutils literal notranslate"><span class="pre">timedelta(0)</span></code>，则名称为 &quot;UTC&quot;，否则为字符串 'UTC±HH:MM'，其中 ± 为 <code class="docutils literal notranslate"><span class="pre">offset</span></code> 值的正负，HH 和 MM 分别为表示 <code class="docutils literal notranslate"><span class="pre">offset.hours</span></code> 和 <code class="docutils literal notranslate"><span class="pre">offset.minutes</span></code> 的两个数码。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.6 版更改: </span>由 <code class="docutils literal notranslate"><span class="pre">offset=timedelta(0)</span></code> 生成的名称现在改为简单的 'UTC' 而不再是 'UTC+00:00'。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="datetime.timezone.dst">
<code class="sig-prename descclassname">timezone.</code><code class="sig-name descname">dst</code><span class="sig-paren">(</span><em class="sig-param">dt</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.timezone.dst" title="永久链接至目标">¶</a></dt>
<dd><p>总是返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
</dd></dl>

<dl class="method">
<dt id="datetime.timezone.fromutc">
<code class="sig-prename descclassname">timezone.</code><code class="sig-name descname">fromutc</code><span class="sig-paren">(</span><em class="sig-param">dt</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.timezone.fromutc" title="永久链接至目标">¶</a></dt>
<dd><p>返回 <code class="docutils literal notranslate"><span class="pre">dt</span> <span class="pre">+</span> <span class="pre">offset</span></code>。 <em>dt</em> 参数必须为一个感知型 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 实例，其中 <code class="docutils literal notranslate"><span class="pre">tzinfo</span></code> 值设为 <code class="docutils literal notranslate"><span class="pre">self</span></code>。</p>
</dd></dl>

<p>类属性：</p>
<dl class="attribute">
<dt id="datetime.timezone.utc">
<code class="sig-prename descclassname">timezone.</code><code class="sig-name descname">utc</code><a class="headerlink" href="#datetime.timezone.utc" title="永久链接至目标">¶</a></dt>
<dd><p>UTC 时区，<code class="docutils literal notranslate"><span class="pre">timezone(timedelta(0))</span></code>。</p>
</dd></dl>

</div>
<div class="section" id="strftime-and-strptime-behavior">
<span id="strftime-strptime-behavior"></span><span id="index-0"></span><h2><code class="xref py py-meth docutils literal notranslate"><span class="pre">strftime()</span></code> 和 <code class="xref py py-meth docutils literal notranslate"><span class="pre">strptime()</span></code> 的行为<a class="headerlink" href="#strftime-and-strptime-behavior" title="永久链接至标题">¶</a></h2>
<p><a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a>, <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 和 <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> 对象都支持 <code class="docutils literal notranslate"><span class="pre">strftime(format)</span></code> 方法，可用来创建一个由指定格式字符串所控制的表示时间的字符串。 总体而言，<code class="docutils literal notranslate"><span class="pre">d.strftime(fmt)</span></code> 类似于 <a class="reference internal" href="time.html#module-time" title="time: Time access and conversions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">time</span></code></a> 模块的 <code class="docutils literal notranslate"><span class="pre">time.strftime(fmt,</span> <span class="pre">d.timetuple())</span></code> 但是并非所有对象都支持 <code class="xref py py-meth docutils literal notranslate"><span class="pre">timetuple()</span></code> 方法。</p>
<p>相反地，<a class="reference internal" href="#datetime.datetime.strptime" title="datetime.datetime.strptime"><code class="xref py py-meth docutils literal notranslate"><span class="pre">datetime.strptime()</span></code></a> 类方法可根据一个表示时间的字符串和对应的格式字符串创建来一个 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象。 <code class="docutils literal notranslate"><span class="pre">datetime.strptime(date_string,</span> <span class="pre">format)</span></code> 等价于 <code class="docutils literal notranslate"><span class="pre">datetime(*(time.strptime(date_string,</span> <span class="pre">format)[0:6]))</span></code>，差别在于当 format 包含小于秒的部分或者时区差值信息的时候，这些信息被 <code class="docutils literal notranslate"><span class="pre">datetime.strptime</span></code> 所支持但会被 <code class="docutils literal notranslate"><span class="pre">time.strptime</span></code> 所丢弃。</p>
<p>对于 <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> 对象，年、月、日的格式代码不应被使用，因为 time 对象没有这些值。 如果它们被使用，年份将被替换为 <code class="docutils literal notranslate"><span class="pre">1900</span></code> 而月和日将被替换为 <code class="docutils literal notranslate"><span class="pre">1</span></code>。</p>
<p>对于 <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 对象，时、分、秒和微秒的格式代码不应被使用，因为 <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 对象没有这些值。 如果它们被使用，它们都将被替换为 <code class="docutils literal notranslate"><span class="pre">0</span></code>。</p>
<p>对于 <a class="reference internal" href="#datetime.datetime.strptime" title="datetime.datetime.strptime"><code class="xref py py-meth docutils literal notranslate"><span class="pre">datetime.strptime()</span></code></a> 类方法，默认值为 <code class="docutils literal notranslate"><span class="pre">1900-01-01T00:00:00.000</span></code>: 任何未在格式字符串中指定的部分都将从默认值中提取。 <a class="footnote-reference brackets" href="#id4" id="id2">2</a></p>
<p>对完整格式代码集的支持在不同平台上有所差异，因为 Python 要调用所在平台 C 库的 <code class="xref py py-func docutils literal notranslate"><span class="pre">strftime()</span></code> 函数，而不同平台的差异是很常见的。 要查看你所用平台所支持的完整格式代码集，请参阅 <em class="manpage">strftime(3)</em> 文档。</p>
<p>出于相同的原因，对于包含当前区域设置字符集所无法表示的 Unicode 码位的格式字符串的处理方式也取决于具体平台。 在某些平台上这样的码位会不加修改地原样输出，而在其他平台上 <code class="docutils literal notranslate"><span class="pre">strftime</span></code> 则可能引发 <a class="reference internal" href="exceptions.html#UnicodeError" title="UnicodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeError</span></code></a> 或只返回一个空字符串。</p>
<p>以下列表显示了 C 标准（1989 版）所要求的全部格式代码，它们在带有标准 C 实现的所有平台上均可用。 请注意 1999 版 C 标准又添加了额外的格式代码。</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 15%" />
<col style="width: 43%" />
<col style="width: 32%" />
<col style="width: 9%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>指令</p></th>
<th class="head"><p>含义</p></th>
<th class="head"><p>示例</p></th>
<th class="head"><p>注释</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%a</span></code></p></td>
<td><p>当地工作日的缩写。</p></td>
<td><div class="line-block">
<div class="line">Sun, Mon, ..., Sat (美国);</div>
<div class="line">So, Mo, ..., Sa (德国)</div>
</div>
</td>
<td><p>(1)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%A</span></code></p></td>
<td><p>当地工作日的全名。</p></td>
<td><div class="line-block">
<div class="line">Sunday, Monday, ..., Saturday (美国);</div>
<div class="line">Sonntag, Montag, ..., Samstag (德国)</div>
</div>
</td>
<td><p>(1)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%w</span></code></p></td>
<td><p>以十进制数显示的工作日，其中0表示星期日，6表示星期六。</p></td>
<td><p>0, 1, ..., 6</p></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%d</span></code></p></td>
<td><p>补零后，以十进制数显示的月份中的一天。</p></td>
<td><p>01, 02, ..., 31</p></td>
<td><p>(9)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%b</span></code></p></td>
<td><p>当地月份的缩写。</p></td>
<td><div class="line-block">
<div class="line">Jan, Feb, ..., Dec (美国);</div>
<div class="line">Jan, Feb, ..., Dez (德国)</div>
</div>
</td>
<td><p>(1)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%B</span></code></p></td>
<td><p>当地月份的全名。</p></td>
<td><div class="line-block">
<div class="line">January, February, ..., December (美国);</div>
<div class="line">Januar, Februar, ..., Dezember (德国)</div>
</div>
</td>
<td><p>(1)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%m</span></code></p></td>
<td><p>补零后，以十进制数显示的月份。</p></td>
<td><p>01, 02, ..., 12</p></td>
<td><p>(9)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%y</span></code></p></td>
<td><p>补零后，以十进制数表示的，不带世纪的年份。</p></td>
<td><p>00, 01, ..., 99</p></td>
<td><p>(9)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%Y</span></code></p></td>
<td><p>十进制数表示的带世纪的年份。</p></td>
<td><p>0001, 0002, ..., 2013,
2014, ..., 9998, 9999</p></td>
<td><p>(2)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%H</span></code></p></td>
<td><p>以补零后的十进制数表示的小时（24 小时制）。</p></td>
<td><p>00, 01, ..., 23</p></td>
<td><p>(9)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%I</span></code></p></td>
<td><p>以补零后的十进制数表示的小时（12 小时制）。</p></td>
<td><p>01, 02, ..., 12</p></td>
<td><p>(9)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%p</span></code></p></td>
<td><p>本地化的 AM 或 PM 。</p></td>
<td><div class="line-block">
<div class="line">AM, PM (美国);</div>
<div class="line">am, pm (德国)</div>
</div>
</td>
<td><p>(1),
(3)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%M</span></code></p></td>
<td><p>补零后，以十进制数显示的分钟。</p></td>
<td><p>00, 01, ..., 59</p></td>
<td><p>(9)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%S</span></code></p></td>
<td><p>补零后，以十进制数显示的秒。</p></td>
<td><p>00, 01, ..., 59</p></td>
<td><p>(4),
(9)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%f</span></code></p></td>
<td><p>以十进制数表示的微秒，在左侧补零。</p></td>
<td><p>000000, 000001, ...,
999999</p></td>
<td><p>(5)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%z</span></code></p></td>
<td><p>±HHMM[SS[.ffffff]] 形式的 UTC 差值（如果对象为简单型则为空字符串）。</p></td>
<td><p>(空), +0000, -0400, +1030, +063415, -030712.345216</p></td>
<td><p>(6)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%Z</span></code></p></td>
<td><p>时区名称（如果对象为简单型则为空字符串）。</p></td>
<td><p>(空), UTC, EST, CST</p></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%j</span></code></p></td>
<td><p>以补零后的十进制数表示的一年中的日序号。</p></td>
<td><p>001, 002, ..., 366</p></td>
<td><p>(9)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%U</span></code></p></td>
<td><p>以补零后的十进制数表示的一年中的周序号（星期日作为每周的第一天）。 在新的一年中第一个星期日之前的所有日子都被视为是在第 0 周。</p></td>
<td><p>00, 01, ..., 53</p></td>
<td><p>(7),
(9)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%W</span></code></p></td>
<td><p>以十进制数表示的一年中的周序号（星期一作为每周的第一天）。 在新的一年中第一个第期一之前的所有日子都被视为是在第 0 周。</p></td>
<td><p>00, 01, ..., 53</p></td>
<td><p>(7),
(9)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%c</span></code></p></td>
<td><p>本地化的适当日期和时间表示。</p></td>
<td><div class="line-block">
<div class="line">Tue Aug 16 21:30:00 1988 (美国);</div>
<div class="line">Di 16 Aug 21:30:00 1988 (德国)</div>
</div>
</td>
<td><p>(1)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%x</span></code></p></td>
<td><p>本地化的适当日期表示。</p></td>
<td><div class="line-block">
<div class="line">08/16/88 (None);</div>
<div class="line">08/16/1988 (en_US);</div>
<div class="line">16.08.1988 (de_DE)</div>
</div>
</td>
<td><p>(1)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%X</span></code></p></td>
<td><p>本地化的适当时间表示。</p></td>
<td><div class="line-block">
<div class="line">21:30:00 (en_US);</div>
<div class="line">21:30:00 (de_DE)</div>
</div>
</td>
<td><p>(1)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%%</span></code></p></td>
<td><p>字面的 <code class="docutils literal notranslate"><span class="pre">'%'</span></code> 字符。</p></td>
<td><p>%</p></td>
<td></td>
</tr>
</tbody>
</table>
<p>有一些并非 C89 标准所要求的额外指令是为使用方便而加入的。 这些形参都与 ISO 8601 日期值相对应。 它们可能不是在所有平台上都可通过 <code class="xref py py-meth docutils literal notranslate"><span class="pre">strftime()</span></code> 方法使用。 ISO 8601 年份和 ISO 8601 星期指令并不能与上面的年份和星期序号指令相互替代。 调用 <code class="xref py py-meth docutils literal notranslate"><span class="pre">strptime()</span></code> 时传入不完整或有歧义的 ISO 8601 指令将引发 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>。</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 15%" />
<col style="width: 43%" />
<col style="width: 32%" />
<col style="width: 9%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>指令</p></th>
<th class="head"><p>含义</p></th>
<th class="head"><p>示例</p></th>
<th class="head"><p>注释</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%G</span></code></p></td>
<td><p>带有世纪的 ISO 8601 年份，表示包含大部分 ISO 星期 (<code class="docutils literal notranslate"><span class="pre">%V</span></code>) 的年份。</p></td>
<td><p>0001, 0002, ..., 2013,
2014, ..., 9998, 9999</p></td>
<td><p>(8)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%u</span></code></p></td>
<td><p>以十进制数显示的 ISO 8601 星期中的日序号，其中 1 表示星期一。</p></td>
<td><p>1, 2, ..., 7</p></td>
<td></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%V</span></code></p></td>
<td><p>以十进制数显示的 ISO 8601 星期，以星期一作为每周的第一天。 第 01 周为包含 1 月 4 日的星期。</p></td>
<td><p>01, 02, ..., 53</p></td>
<td><p>(8),
(9)</p></td>
</tr>
</tbody>
</table>
<div class="versionadded">
<p><span class="versionmodified added">3.6 新版功能: </span>增加了 <code class="docutils literal notranslate"><span class="pre">%G</span></code>,  <code class="docutils literal notranslate"><span class="pre">%u</span></code>  和 <code class="docutils literal notranslate"><span class="pre">%V</span></code>。</p>
</div>
<p>注释:</p>
<ol class="arabic">
<li><p>由于此格式依赖于当前区域设置，因此对具体输出值应当保持谨慎预期。 字段顺序会发生改变（例如 &quot;month/day/year&quot; 与 &quot;day/month/year&quot;），并且输出可能包含使用区域设置所指定的默认编码格式的 Unicode 字符（例如如果当前区域为 <code class="docutils literal notranslate"><span class="pre">ja_JP</span></code>，则默认编码格式可能为 <code class="docutils literal notranslate"><span class="pre">eucJP</span></code>, <code class="docutils literal notranslate"><span class="pre">SJIS</span></code> 或 <code class="docutils literal notranslate"><span class="pre">utf-8</span></code> 中的一个；使用 <a class="reference internal" href="locale.html#locale.getlocale" title="locale.getlocale"><code class="xref py py-meth docutils literal notranslate"><span class="pre">locale.getlocale()</span></code></a> 可确定当前区域设置的编码格式）。</p></li>
<li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">strptime()</span></code> 方法能够解析整个 [1, 9999] 范围内的年份，但 &lt; 1000 的年份必须加零填充为 4 位数字宽度。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.2 版更改: </span>在之前的版本中，<code class="xref py py-meth docutils literal notranslate"><span class="pre">strftime()</span></code> 方法只限于 &gt;= 1900 的年份。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.3 版更改: </span>在版本3.2中，<code class="xref py py-meth docutils literal notranslate"><span class="pre">strftime()</span></code> 方法只限于 years &gt;= 1000。</p>
</div>
</li>
<li><p>当与 <code class="xref py py-meth docutils literal notranslate"><span class="pre">strptime()</span></code> 方法一起使用时，如果使用 <code class="docutils literal notranslate"><span class="pre">%I</span></code> 指令来解析小时，<code class="docutils literal notranslate"><span class="pre">%p</span></code> 指令只影响输出小时字段。</p></li>
<li><p>与 <a class="reference internal" href="time.html#module-time" title="time: Time access and conversions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">time</span></code></a> 模块不同的是， <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code></a> 模块不支持闰秒。</p></li>
<li><p>当与 <code class="xref py py-meth docutils literal notranslate"><span class="pre">strptime()</span></code> 方法一起使用时，<code class="docutils literal notranslate"><span class="pre">%f</span></code> 指令可接受一至六个数码及左边的零填充。  <code class="docutils literal notranslate"><span class="pre">%f</span></code> 是对 C 标准中格式字符集的扩展（但单独在 datetime 对象中实现，因此它总是可用）。</p></li>
<li><p>对于简单型对象，<code class="docutils literal notranslate"><span class="pre">%z</span></code> and <code class="docutils literal notranslate"><span class="pre">%Z</span></code> 格式代码会被替换为空字符串。</p>
<p>对于一个觉悟型对象而言：</p>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">%z</span></code></dt><dd><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">utcoffset()</span></code> 会被转换为 ±HHMM[SS[.ffffff]] 形式的字符串，其中 HH 为给出 UTC 差值小时部分的 2 位数码字符串，MM 为给出 UTC 差值分钟部分的 2 位数码字符串，SS 为给出 UTC 差值秒部分的2 位数码字符串，而 ffffff 为给出 UTC 差值微秒部分的 6 位数码字符串。 当差值为整数秒时 ffffff 部分将被省略，而当差值为整数分钟时 ffffff 和 SS 部分都将被省略。 例如，如果 <code class="xref py py-meth docutils literal notranslate"><span class="pre">utcoffset()</span></code> 返回 <code class="docutils literal notranslate"><span class="pre">timedelta(hours=-3,</span> <span class="pre">minutes=-30)</span></code>, <code class="docutils literal notranslate"><span class="pre">%z</span></code> 会被替换为字符串 <code class="docutils literal notranslate"><span class="pre">'-0330'</span></code>。</p>
</dd>
</dl>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.7 版更改: </span>UTC 时差不再限制为一个整数分钟值。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.7 版更改: </span>当提供 <code class="docutils literal notranslate"><span class="pre">%z</span></code> 指令给 <code class="xref py py-meth docutils literal notranslate"><span class="pre">strptime()</span></code> 方法时，UTC 差值可以在时、分和秒之间使用冒号分隔符。 例如，<code class="docutils literal notranslate"><span class="pre">'+01:00:00'</span></code> 将被解读为一小时的差值。 此外，提供 <code class="docutils literal notranslate"><span class="pre">'Z'</span></code> 就相当于 <code class="docutils literal notranslate"><span class="pre">'+00:00'</span></code>。</p>
</div>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">%Z</span></code></dt><dd><p>如果 <code class="xref py py-meth docutils literal notranslate"><span class="pre">tzname()</span></code> 返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>，<code class="docutils literal notranslate"><span class="pre">%Z</span></code> 会被替换为一个空字符串。 在其他情况下 <code class="docutils literal notranslate"><span class="pre">%Z</span></code> 会被替换为返回值，这必须为一个字符串。</p>
</dd>
</dl>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.2 版更改: </span>当提供 <code class="docutils literal notranslate"><span class="pre">%z</span></code> 指令给 <code class="xref py py-meth docutils literal notranslate"><span class="pre">strptime()</span></code> 方法时，将产生一个感知型 <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象。 结果的 <code class="docutils literal notranslate"><span class="pre">tzinfo</span></code> 将被设为一个 <a class="reference internal" href="#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a> 实例。</p>
</div>
</li>
<li><p>当与 <code class="xref py py-meth docutils literal notranslate"><span class="pre">strptime()</span></code> 方法一起使用时，<code class="docutils literal notranslate"><span class="pre">%U</span></code> 和 <code class="docutils literal notranslate"><span class="pre">%W</span></code> 仅用于指定星期几和日历年份 (<code class="docutils literal notranslate"><span class="pre">%Y</span></code>) 的计算。</p></li>
<li><p>类似于 <code class="docutils literal notranslate"><span class="pre">%U</span></code> 和 <code class="docutils literal notranslate"><span class="pre">%W</span></code>，<code class="docutils literal notranslate"><span class="pre">%V</span></code> 仅用于在 <code class="xref py py-meth docutils literal notranslate"><span class="pre">strptime()</span></code> 格式字符串中指定星期几和 ISO 年份 (<code class="docutils literal notranslate"><span class="pre">%G</span></code>) 的计算。 还要注意 <code class="docutils literal notranslate"><span class="pre">%G</span></code> 和 <code class="docutils literal notranslate"><span class="pre">%Y</span></code> 是不可交换的。</p></li>
<li><p>当于 <code class="xref py py-meth docutils literal notranslate"><span class="pre">strptime()</span></code> 方法一起使用时，前导的零在格式 <code class="docutils literal notranslate"><span class="pre">%d</span></code>, <code class="docutils literal notranslate"><span class="pre">%m</span></code>, <code class="docutils literal notranslate"><span class="pre">%H</span></code>, <code class="docutils literal notranslate"><span class="pre">%I</span></code>, <code class="docutils literal notranslate"><span class="pre">%M</span></code>, <code class="docutils literal notranslate"><span class="pre">%S</span></code>, <code class="docutils literal notranslate"><span class="pre">%J</span></code>, <code class="docutils literal notranslate"><span class="pre">%U</span></code>, <code class="docutils literal notranslate"><span class="pre">%W</span></code> 和 <code class="docutils literal notranslate"><span class="pre">%V</span></code> 中是可选的。 格式 <code class="docutils literal notranslate"><span class="pre">%y</span></code> 不要求有前导的零。</p></li>
</ol>
<p class="rubric">备注</p>
<dl class="footnote brackets">
<dt class="label" id="id3"><span class="brackets"><a class="fn-backref" href="#id1">1</a></span></dt>
<dd><p>就是说如果我们忽略相对论效应的话。</p>
</dd>
<dt class="label" id="id4"><span class="brackets"><a class="fn-backref" href="#id2">2</a></span></dt>
<dd><p>传入 <code class="docutils literal notranslate"><span class="pre">datetime.strptime('Feb</span> <span class="pre">29',</span> <span class="pre">'%b</span> <span class="pre">%d')</span></code> 将导致错误，因为 <code class="docutils literal notranslate"><span class="pre">1900</span></code> 不是闰年。</p>
</dd>
</dl>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">目录</a></h3>
  <ul>
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code> --- 基本的日期和时间类型</a><ul>
<li><a class="reference internal" href="#available-types">有效的类型</a></li>
<li><a class="reference internal" href="#timedelta-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code> 类对象</a></li>
<li><a class="reference internal" href="#date-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code> 对象</a></li>
<li><a class="reference internal" href="#datetime-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code> 对象</a></li>
<li><a class="reference internal" href="#time-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code> 对象</a></li>
<li><a class="reference internal" href="#tzinfo-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code> 对象</a></li>
<li><a class="reference internal" href="#timezone-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code> 对象</a></li>
<li><a class="reference internal" href="#strftime-and-strptime-behavior"><code class="xref py py-meth docutils literal notranslate"><span class="pre">strftime()</span></code> 和 <code class="xref py py-meth docutils literal notranslate"><span class="pre">strptime()</span></code> 的行为</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="datatypes.html"
                        title="上一章">数据类型</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="calendar.html"
                        title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">calendar</span></code> --- 日历相关函数</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/library/datetime.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="calendar.html" title="calendar --- 日历相关函数"
             >下一页</a> |</li>
        <li class="right" >
          <a href="datatypes.html" title="数据类型"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="datatypes.html" >数据类型</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2020, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 6月 29, 2020.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>2.3.1 创建。
    </div>

  </body>
</html>